> db.foo.find() { "_id" : ObjectId("5389aa1df06b88aaa313746a"), "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "_id" : ObjectId("5389aaa4afce65313a5614f7"), "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "_id" : ObjectId("5389aabaafce65313a5614f8"), "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "_id" : ObjectId("5389aac5afce65313a5614f9"), "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
第一个{} 放where条件 第二个{} 指定哪些列显示和不显示 (0表示不显示 >0表示显示)
后面演示使用{‘_id‘:0} 默认隐藏‘_id列’减少显示量
> db.foo.find({},{'_id':0,'name':1,'user':1}) { "name" : "yiwa", "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "user" : { "phone" : [ 63, 137 ] } }
#名字是yiwa且年龄是25岁
> db.foo.find({'name':'yiwa','age':25},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
> db.foo.find({'$or':[{'name':'yiwa'},{'age':75}]},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
#年龄在 15<= x <=75 岁
> db.foo.find({'age':{'$gte':15,'$lte':75}},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
# 对age%3==1的取模结果
> db.foo.find({'age':{'$mod':[3,1]}},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
> db.foo.find({'name':{'$nin':['siwa']},'age':{'$in':[15,25,85]}},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
> db.foo.find({'name':null},{'_id':0}) >
> db.foo.find({'name':/iwa/},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.find({'name':/^yi/},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
> db.foo.distinct('name') [ "yiwa", "erwa", "sanwa", "siwa" ] > db.foo.count() 4
> db.foo.find({'user.phone':186},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
> db.foo.find({'user.phone':{'$all':[188,186]}},{'_id':0}) { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
> db.foo.find({'user.phone':{'$size':2}},{'_id':0}) { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({'name':{'$type':2},'age':{'$type':16}},{'_id':0}).sort({'name':1,'age':-1}) { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }#name中值存在的:true
> db.foo.find({'name':{'$exists':true}},{'_id':0}) { "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.find({'name':{'$exists':false}},{'_id':0}) >
> db.foo.save({x:[{'a':1,'b':5},999,'liw',{'a':12},{'b':100}]}) WriteResult({ "nInserted" : 1 })#查询某元素中a=1,b=5的元素
> db.foo.find({'x':{'$elemMatch':{'a':1,b:{'$gt':4}}}},{'_id':0}) { "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] } > db.foo.find({'x.a':1,'x.b':5},{'_id':0}) { "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }
> db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.update({'age':{$gte:30}},{$set:{'age':55}},fasle,false) 2014-05-31T19:36:05.407+0800 ReferenceError: fasle is not defined > db.foo.update({'age':{$gte:30}},{$set:{'age':55}},false,false) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.update({'age':{$gte:30}},{$set:{'age':56}},false,false) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.update({'age':{$gte:300}},{$set:{'age':56}},true,false) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("5389bee8afce65313a5614fa") }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } { "age" : 56 } > db.foo.update({'age':{$gte:30}},{$set:{'age':56}},true,true) WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 2 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } { "age" : 56 }
> db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 58, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.update({'age':{$gte:30}},{$inc:{'age':20}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } > db.foo.update({'age':56},{$set:{'name':'laoda','age':65}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }#只更新了一条数据,因为 multi 默认为false
> db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } { "age" : 65, "name" : "laoda" } > db.foo.update({'name':'laoda','user':{$exists:true}},{$unset:{"age":65}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.foo.find({},{'_id':0}) { "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } } { "name" : "laoda", "user" : { "phone" : [ 63, 188, 13, 186 ] } } { "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } } { "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } } { "age" : 65, "name" : "laoda" }
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda" } > db.array.update({'name':'laoda','age':65},{$push:{"phone":65}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 65 ] } > db.array.update({'name':'laoda','age':65},{$push:{"phone":[65,75,{'iphone':'188'},85]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 65, [ 65, 75, { "iphone" : "188" }, 85 ] ] }
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda" } > db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222 ] } > db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222 ] }
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] } > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] } > db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] } > db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] } > db.array.update({'name':'laoda','age':65},{$pop:{"phone":1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] } > db.array.update({'name':'laoda','age':65},{$pop:{"phone":-1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222, [ 111, 222 ] ] } > db.array.update({'name':'laoda','age':65},{$pop:{"phone":2}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222 ] } > db.array.update({'name':'laoda','age':65},{$pop:{"phone":333}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 222, 111 ] } > db.array.update({'name':'laoda','age':65},{$pop:{"phone":-333}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111 ] }#测试发现,只要是正整数从最后删除,负数从头部删除。
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 333 ] } > db.array.update({'name':'laoda','age':65},{$pull:{"phone":333}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111 ] }
> db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 111, 333, 222 ] } > db.array.update({'name':'laoda','age':65},{$pullAll:{"phone":[111,222]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.array.find({},{'_id':0}) { "age" : 65, "name" : "laoda", "phone" : [ 333 ] }
Mongodb数据库命令端常用操作,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011278496/article/details/27836729