标签:
> db.test.find()
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c86d3017c5eb7d08aed6d"), "name" : "bob", "age" : 1, "status" : "done" }
{ "_id" : ObjectId("573c88fe017c5eb7d08aed6e"), "name" : "tom", "age" : 10, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
> db.test.find({"name":"brent"})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30, "status" : "done" }
> db.test.find({"name":"brent","age":43})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43, "status" : "done" }
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
> db.test.find({"name":"brent"},{"status":0})
{ "_id" : ObjectId("573c858c323f7f2e2ccb0e17"), "name" : "brent", "age" : 43 }
{ "_id" : ObjectId("573c8bd3323f7f2e2ccb0e18"), "name" : "brent", "age" : 30 }
> db.test.find({"name":"brent"},{"status":0,"_id":0})
{ "name" : "brent", "age" : 43 }
{ "name" : "brent", "age" : 30 }
> db.test2.find()
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"age":{"$gt":20,"$lt":30}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
> db.test2.find({"name":{"$ne":"brent"}})
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"age":{"$in":[10,14]}})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"age":{"$nin":[10,14]}})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
> db.test2.find({"$or":[{"name":"bob"},{"age":10}]})
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.find({"$or":[{"age":{"$in":[10,28]}},{"name":"bob"}]})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
> db.test2.insert({"name":null})
WriteResult({ "nInserted" : 1 })
> db.test2.find()
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
{ "_id" : ObjectId("573e80569e178b5475b29d8c"), "name" : null }
> db.test2.find({"name":null})
{ "_id" : ObjectId("573e80569e178b5475b29d8c"), "name" : null }
> db.test2.find({"job":null})
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
{ "_id" : ObjectId("573e80569e178b5475b29d8c"), "name" : null }
> db.test2.find({"job" : {"$in":[null],"$exists":true}})
> db.test2.find({"name":/bob/i})
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
{ "_id" : ObjectId("573e853c9e178b5475b29d8d"), "name" : "BOB" }
> db.test3.find()
{ "_id" : ObjectId("573e874c9e178b5475b29d8e"), "name" : "brent", "fav" : [ "game", "film", "read" ] }
{ "_id" : ObjectId("573e87849e178b5475b29d8f"), "name" : "bob", "fav" : [ "drink", "football", "runing" ] }
{ "_id" : ObjectId("573e87cc9e178b5475b29d90"), "name" : "jack", "fav" : [ "read", "basketball", "drink" ] }
> db.test3.find({"fav":"drink"})
{ "_id" : ObjectId("573e87849e178b5475b29d8f"), "name" : "bob", "fav" : [ "drink", "football", "runing" ] }
{ "_id" : ObjectId("573e87cc9e178b5475b29d90"), "name" : "jack", "fav" : [ "read", "basketball", "drink" ] }
> db.test3.find({"fav":{"$all":["drink","read"]}})
{ "_id" : ObjectId("573e87cc9e178b5475b29d90"), "name" : "jack", "fav" : [ "read", "basketball", "drink" ] }
> db.test3.find({"fav":["drink"]})
> db.test3.find({"fav":["drink","running","football"]})
> db.test3.find({"fav":["drink","football","runing"]})
{ "_id" : ObjectId("573e87849e178b5475b29d8f"), "name" : "bob", "fav" : [ "drink", "football", "runing" ] }
> db.test3.find({"fav.1":"film"})
{ "_id" : ObjectId("573e874c9e178b5475b29d8e"), "name" : "brent", "fav" : [ "game", "film", "read" ] }
> db.test3.find({"fav":{"$size":2}})
{ "_id" : ObjectId("573e8b719e178b5475b29d91"), "name" : "tom", "fav" : [ "chess", "cooking" ] }
> db.test3.findOne({"name":"brent"},{"fav":{"$slice":2}})
{
"_id" : ObjectId("573e874c9e178b5475b29d8e"),
"name" : "brent",
"fav" : [
"game",
"film"
]
> db.test3.findOne({"name":"brent"},{"fav":{"$slice":[1,2]}})
{
"_id" : ObjectId("573e874c9e178b5475b29d8e"),
"name" : "brent",
"fav" : [
"film",
"read"
]
}
> db.test4.findOne()
{
"_id" : ObjectId("573f390c9e178b5475b29d92"),
"name" : "brent",
"comment" : [
{
"name" : "bob",
"content" : "good"
},
{
"name" : "jack",
"content" : "repost"
}
]
}
> db.test4.find({"comment.name":"bob"},{"comment.$":1})
{ "_id" : ObjectId("573f390c9e178b5475b29d92"), "comment" : [ { "name" : "bob", "content" : "good" } ] }
> db.test.find({"x":{"$elemMatch":{"$gt":10,"$lt",20}}})
> db.test.find({"x":{"$gt":10,"$lt":20}}).min({"x":10}).max({"x":20})
> db.test5.findOne()
{
"_id" : ObjectId("5740142abc583612a8464117"),
"name" : {
"first" : "kevin",
"last" : "love"
}
}
> db.test5.find({"name.first":"kevin","name.last":"love"})
{ "_id" : ObjectId("5740142abc583612a8464117"), "name" : { "first" : "kevin", "last" : "love" } }
> for(i=0;i<100;i++){
... db.t1.insert({x:i})
... }
WriteResult({ "nInserted" : 1 })
> var cur=db.t1.find()
> while(cur.hasNext()){
... obj=cur.next();
... }
{ "_id" : ObjectId("57401df5bc583612a846417b"), "x" : 99 }
> var cur=db.t1.find()
> cur.forEach(function(x){
... print(x.name);
... });
> var cur=db.t1.find().sort({"x":1}).limit(1).skip(10);
> var cur=db.t1.find().limit(1).sort({"x":1}).skip(10);
> var cur=db.t1.find().skip(10).limit(1).sort({"x":1});
> db.t1.find().limit(3)
{ "_id" : ObjectId("57401df5bc583612a8464118"), "x" : 0 }
{ "_id" : ObjectId("57401df5bc583612a8464119"), "x" : 1 }
{ "_id" : ObjectId("57401df5bc583612a846411a"), "x" : 2 }
> db.t1.find().skip(99)
{ "_id" : ObjectId("57401df5bc583612a846417b"), "x" : 99 }
> db.test2.find().sort({"name":1,"age":-1})
{ "_id" : ObjectId("573e80569e178b5475b29d8c"), "name" : null }
{ "_id" : ObjectId("573e853c9e178b5475b29d8d"), "name" : "BOB" }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14 }
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28 }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10 }
> db.stock.find({"desc":"mp3"}).limit(50).sort({"price":-1})
> db.stock.find({"desc":"mp3"}).limit(50).sort({"price":-1}).skip(50)
while(page1.hasNext()){
latest=page1.next();
display(latest)
}
var page2=db.stock.find({"price":{"$lt":latest.price}});
pages.sort({"price":1}).limit(100);
> db.test2.find({"name":"brent"})
> db.t1.find({"name":"brent"}).sort({"x":1})
> db.t1.find()._addSpecial("$maxscan",1)
> db.test2.find()._addSpecial("$showDiskLoc",true)
{ "_id" : ObjectId("573e72449e178b5475b29d89"), "name" : "brent", "age" : 28, "$recordId" : NumberLong(1) }
{ "_id" : ObjectId("573e73149e178b5475b29d8a"), "name" : "brent", "age" : 10, "$recordId" : NumberLong(2) }
{ "_id" : ObjectId("573e73ae9e178b5475b29d8b"), "name" : "bob", "age" : 14, "$recordId" : NumberLong(3) }
{ "_id" : ObjectId("573e80569e178b5475b29d8c"), "name" : null, "$recordId" : NumberLong(4) }
{ "_id" : ObjectId("573e853c9e178b5475b29d8d"), "name" : "BOB", "$recordId" : NumberLong(5) }
> db.runCommand({"drop":"t1"})
{ "ns" : "suq.t1", "nIndexesWas" : 1, "ok" : 1 }
> db.runCommand({"drop":"t1"})
{ "ok" : 0, "errmsg" : "ns not found", "code" : 26 }
> db.$cmd.findOne({"drop":"t1"})
{ "ok" : 0, "errmsg" : "ns not found", "code" : 26 }
> db.adminCommand({shutdown:1})
2016-05-21T21:28:43.942+0800 E QUERY [thread1] Error: error doing query: failed: network error while attempting to run command ‘shutdown‘ on host ‘127.0.0.1:27017‘ :
DB.prototype.runCommand@src/mongo/shell/db.js:135:1
DB.prototype.adminCommand@src/mongo/shell/db.js:153:16
@(shell):1:1
2016-05-21T21:28:43.943+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-05-21T21:28:43.944+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-05-21T21:28:43.944+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
标签:
原文地址:http://blog.csdn.net/su377486/article/details/51472519