标签:for 处理 next ups views 产品 selector 内存映射 非关系数据库
3. Saving
Here‘s how you save a document to MongoDB:
db.scores.save({a: 99});
This says, "save the document ‘{a: 99}‘ to the ‘scores‘ collection."
Go ahead and try it. Then, to see if the document was saved, try
db.scores.find();
Once you‘ve tried this, type ‘next‘.
>db.scores.save({a:99});
"ok"
>db.me.save({name:‘tom‘,language:[‘java‘,‘c‘,‘python‘]});
"ok"
>db.me.find();
[
{ "name" : "tom", "language" : [ "java", "c", "python" ], "_id" : { "$oid" : "52156671cc93742c160c66bc" } }
]
>db.scores.find();
[
{ "a" : 99, "_id" : { "$oid" : "52156646cc93742c160c66bb" } }
]
>
4. Saving and Querying
Try adding some documents to the scores collection:
for(i=0; i<10; i++) { db.scores.save({a: i, exam: 5}) };
Try that, then enter
db.scores.find();
to see if the save succeeded. Since the shell only displays 10 results at time,
you‘ll need to enter the ‘it‘ command to iterate over the rest.
(enter ‘next‘ when you‘re ready)
>for(i=0;i<20;i++){db.scores.save({student:i,score:20})};
"ok"
>db.scores.find();
[
{ "a" : 99, "_id" : { "$oid" : "52156646cc93742c160c66bb" } },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c0" }, "student" : 3, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c1" }, "student" : 1, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c2" }, "student" : 0, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c3" }, "student" : 2, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c4" }, "student" : 5, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c5" }, "student" : 4, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c6" }, "student" : 7, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c7" }, "student" : 6, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c8" }, "student" : 9, "score" : 20 }
]
>it
[
{ "_id" : { "$oid" : "52156720cc93742c160c66c9" }, "student" : 10, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66ca" }, "student" : 11, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66cb" }, "student" : 12, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66cc" }, "student" : 13, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66cd" }, "student" : 14, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66ce" }, "student" : 8, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66cf" }, "student" : 15, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d0" }, "student" : 16, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d1" }, "student" : 17, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d2" }, "student" : 19, "score" : 20 }
]
>it
[
{ "_id" : { "$oid" : "52156721cc93742c160c66d3" }, "student" : 18, "score" : 20 }
]
>
5. Basic Queries
You‘ve already tried a few queries, but let‘s make them more specific.
How about finding all documents where a == 2:
db.scores.find({a: 2});
Or what about documents where a > 15?
db.scores.find({a: {‘$gt‘: 15}});
>db.scores.find({student:2});
[
{ "_id" : { "$oid" : "5215671fcc93742c160c66c3" }, "student" : 2, "score" : 20 }
]
>db.scores.find({a:{‘$gt‘:12}});
[
{ "a" : 99, "_id" : { "$oid" : "52156646cc93742c160c66bb" } }
]
>db.scores.find({student:{‘$gt‘:12}});
[
{ "_id" : { "$oid" : "52156721cc93742c160c66cc" }, "student" : 13, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66cd" }, "student" : 14, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66cf" }, "student" : 15, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d0" }, "student" : 16, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d1" }, "student" : 17, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d2" }, "student" : 19, "score" : 20 },
{ "_id" : { "$oid" : "52156721cc93742c160c66d3" }, "student" : 18, "score" : 20 }
]
>db.scores.find({student:{‘$lt‘:12}});
[
{ "_id" : { "$oid" : "5215671fcc93742c160c66c0" }, "student" : 3, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c1" }, "student" : 1, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c2" }, "student" : 0, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c3" }, "student" : 2, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c4" }, "student" : 5, "score" : 20 },
{ "_id" : { "$oid" : "5215671fcc93742c160c66c5" }, "student" : 4, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c6" }, "student" : 7, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c7" }, "student" : 6, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c8" }, "student" : 9, "score" : 20 },
{ "_id" : { "$oid" : "52156720cc93742c160c66c9" }, "student" : 10, "score" : 20 }
]
>db.scores.find({student:{‘$eq‘:12}});
[
]
>
6. Query Operators
Query Operators:
$gt is one of many special query operators. Here are few others:
$lt - ‘<‘, $lte - ‘<=‘,
$gte - ‘>=‘, $ne - ‘!=‘
$in - ‘is in array‘, $nin - ‘! in array‘
db.scores.find({a: {‘$in‘: [2, 3, 4]}});
db.scores.find({a: {‘$gte‘: 2, ‘$lte‘: 4}});
Try creating some queries, then type ‘next.‘
>db.scores.find({student:{‘$in‘:[12]}});
[
{ "_id" : { "$oid" : "52156720cc93742c160c66cb" }, "student" : 12, "score" : 20 }
]
>db.scores.save({student:‘tom‘,age:15,language:[‘java‘,‘c‘,‘ruby‘]});
"ok"
>db.scores.save({student:‘tom1‘,age:16,language:[‘java‘,‘c‘,‘python‘]});
"ok"
>db.scores.save({student:‘tom2‘,age:16,language:[‘c‘,‘python‘]});
"ok"
>db.scores.find({language:{‘$in‘:[‘c‘]}});
[
{ "language" : [ "java", "c", "ruby" ], "_id" : { "$oid" : "52156a7ecc93742c160c66da" }, "student" : "tom", "age" : 15 },
{ "language" : [ "java", "c", "python" ], "_id" : { "$oid" : "52156a8fcc93742c160c66db" }, "student" : "tom1", "age" : 16 },
{ "language" : [ "c", "python" ], "_id" : { "$oid" : "52156a9dcc93742c160c66dc" }, "student" : "tom2", "age" : 16 }
]
>db.scores.find({language:{‘$in‘:[‘python‘]}});
[
{ "language" : [ "java", "c", "python" ], "_id" : { "$oid" : "52156a8fcc93742c160c66db" }, "student" : "tom1", "age" : 16 },
{ "language" : [ "c", "python" ], "_id" : { "$oid" : "52156a9dcc93742c160c66dc" }, "student" : "tom2", "age" : 16 }
]
>
7. Updates
Now create a couple documents like these for updating:
db.users.save({name: ‘Johnny‘, languages: [‘ruby‘, ‘c‘]});
db.users.save({name: ‘Sue‘, languages: [‘scala‘, ‘lisp‘]});
Make sure they were saved by called db.users.find()
Update the first document like so:
db.users.update({name: ‘Johnny‘}, {name: ‘Cash‘, languages: [‘english‘]});
>db.users.save({name:‘Johnny‘,language:[‘java‘,‘c‘]});
"ok"
>db.users.save({name:‘Tome‘,language:[‘python‘,‘java‘]});
"ok"
>db.users.find();
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Tome", "language" : [ "python", "java" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" } }
]
>db.users.find({name:‘Tome‘});
[
{ "name" : "Tome", "language" : [ "python", "java" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" } }
]
>db.users.update({name:‘Tome‘},{name:‘Jack‘,language:[‘english‘]});
"ok"
>db.users.find();
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" } }
]
>
8. Update Operators
The previous update replaced the entire document, but MongoDB also
supports partial updates to documents. For example, you can set a value:
db.users.update({name: ‘Cash‘}, {‘$set‘: {‘age‘: 50} });
You can also push and pull items from arrays:
db.users.update({name: ‘Sue‘}, {‘$pull‘: {‘languages‘: ‘scala‘} });
db.users.update({name: ‘Sue‘}, {‘$push‘: {‘languages‘: ‘ruby‘} });
Give these a try, check the results, and then enter ‘next‘.
>db.users.update({name:‘Jack‘},{‘$set‘:{‘age‘:50}});
"ok"
>db.users.find();
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 50 }
]
>db.users.update({name:‘Jack‘},{‘$pull‘:{language:‘java‘}});
"ok"
>db.users.find();
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 50 }
]
>db.users.find();
"ok"
>db.users.update({name:‘Jack‘},{‘$pull‘:{language:‘java‘}});
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english", "java" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 50 }
]
>db.users.find();
"ok"
>db.users.update({name:‘Jack‘},{‘$set‘:{age:60}});
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 50 }
]
>db.users.update({name:‘Jack‘},{‘$set‘:{age:60}});
"ok"
>db.users.find();
[
{ "name" : "Johnny", "language" : [ "java", "c" ], "_id" : { "$oid" : "52156b66cc93742c160c66df" } },
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 60 }
]
>
9. Deleting data
To delete matching documents only, add a query selector to the remove method:
db.users.remove({name: ‘Sue‘});
To delete everything from a collection:
db.scores.remove();
>db.users.remove({name:‘Johnny‘});
"ok"
>db.users.find();
[
{ "name" : "Jack", "language" : [ "english" ], "_id" : { "$oid" : "52156b8bcc93742c160c66e2" }, "age" : 60 }
]
>db.users.remove();
"ok"
>db.users.find();
[
]
6、MongoDB与RDBMS的比较与使用场景
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
标签:for 处理 next ups views 产品 selector 内存映射 非关系数据库
原文地址:https://www.cnblogs.com/skiwdhwhssh/p/10341732.html