mongo 语法 增删改查
1.增
db.collection.insert()与db.collection.save() 都是增加,区别:save()遇到相同_id后,则更新此_id数据。 而insert()则报错
> db.c1.find() > db.c1.insert({_id:1, name:'use001'}); > db.c1.insert({_id:1, name:'use001'}); E11000 duplicate key error index: test.c1.$_id_ dup key: { : 1.0 } > db.c1.save({_id:1, name:'user002'}); > db.c1.save({_id:1, name:'user003'}); > db.c1.find(); { "_id" : 1, "name" : "user003" }
2.查
db.collection.find()//不带条件 查询所有
db.collection.find({name:'user007'})//条件查找 某些个
> db.c1.insert({name:'user11', age:22, sex:'man'}); > db.c1.find({name:'user11'}, {name:1});//find 第二个条件参数 { "_id" : ObjectId("51e221897d2b412f74eefb68"), "name" : "user11" } > db.c1.find({name:'user11'}, {name:1, _id:0});//_id都不要,0表示不获取,1表示要获取的 { "name" : "user11" }
//条件表达式,$gt $gte $lt $lte $ne $mod取余 $in $nin $size数组元素个数 $exists:0,:1 $slice 正则匹配 魔术方法均在json内侧 > db.c1.find({age:{$gt:5}}, {_id:0}); { "name" : "user6", "age" : 6 } { "name" : "user7", "age" : 7 } { "name" : "user8", "age" : 8 } { "name" : "user9", "age" : 9 }
> db.c1.find({name:/user1/i});//正则匹配 i 忽略大小写 符合perl规则
{ "_id" : ObjectId("51e229047d2b412f74eefb73"), "name" : "user1" }
{ "_id" : ObjectId("51e22b277d2b412f74eefb7c"), "name" : "user10" }
> db.c2.find({},{age:{$slice:2}});//$slice 在第二个参数框,做条件
{ "_id" : ObjectId("51e2324c7d2b412f74eefb7d"), "age" : [ 1111, 2222 ] }
> for(i=1;i<10;i++){ ... db.c1.insert({name:'user'+i});} > db.c1.find(); { "_id" : ObjectId("51e22008866f862cf4662252"), "name" : "user1" } { "_id" : ObjectId("51e22008866f862cf4662253"), "name" : "user2" } { "_id" : ObjectId("51e22008866f862cf4662254"), "name" : "user3" } { "_id" : ObjectId("51e22008866f862cf4662255"), "name" : "user4" } { "_id" : ObjectId("51e22008866f862cf4662256"), "name" : "user5" } { "_id" : ObjectId("51e22008866f862cf4662257"), "name" : "user6" } { "_id" : ObjectId("51e22008866f862cf4662258"), "name" : "user7" } { "_id" : ObjectId("51e22008866f862cf4662259"), "name" : "user8" } { "_id" : ObjectId("51e22008866f862cf466225a"), "name" : "user9" } //MONGO 语法 完全JS引擎
db.collection.findOne()
.sort({age:-1}) //降序
.skip().limit()
.count()默认<=>count(0) 区别于 count(1)
//post 为 数组 [] > db.c1.insert({name:'user1', post:[1,2,3,4,5,6,7,8,9]}); > db.c1.find(); { "_id" : ObjectId("51e225f17d2b412f74eefb72"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] } //$all 查找包含 [1,4,7,9] 元素的 post > db.c1.find({post:{$all:[1,4,7,9]}}); { "_id" : ObjectId("51e225f17d2b412f74eefb72"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
//$or $nor 2个特殊魔术方法,方法体在json最左侧 而非右侧 > db.c1.find({$or:[{name:'user1'}, {name:'user4'}]}); { "_id" : ObjectId("51e229047d2b412f74eefb73"), "name" : "user1" } { "_id" : ObjectId("51e229047d2b412f74eefb76"), "name" : "user4" }
distinct 取唯一值:
> db.c1.distinct('name'); [ "user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9", "user10" ]
游标:mysql类似
> var x = db.c1.find() > x.hasNext(); true > x.next(); { "_id" : ObjectId("51e229047d2b412f74eefb73"), "name" : "user1" }
3.删
db.collection.remove();//删除所有文档集
db.collection.remove({name:'user001'});//删除指定
4.改
db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。 默认0
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。 默认0
> db.c3.find(); { "_id" : ObjectId("51e23998b84469c6b5ccfa5f"), "name" : "user1", "age" : 22 } > db.c3.update({name:'user1'}, {$set:{name:'user22'}});//带$set参数 > db.c3.find(); { "_id" : ObjectId("51e23998b84469c6b5ccfa5f"), "age" : 22, "name" : "user22" } > db.c3.update({name:'user22'}, {name:'wowo'});//不带$set > db.c3.find(); { "_id" : ObjectId("51e23998b84469c6b5ccfa5f"), "name" : "wowo" }
一次性修改多条数据:
db.c3.update({name:'user1'}, {$set:{age:11, sex:'boy'}});
$inc 如果不存在score, inc自动创建此score
> db.c4.insert({name:'wwwwwwww'}); > db.c4.update({},{$inc:{score:10}}, 0, 1); //0 1 // 1 1都可以 > db.c4.find(); { "_id" : ObjectId("51e23da6b84469c6b5ccfa60"), "name" : "wwwwwwww", "score" : 10 }
$unset 删除某一个字段
> db.c1.update({}, {$unset:{name:1}}, 0, 1); //对集合c1 下所有文档 进行 删除name元素的 操作
$push针对数组操作-添加一个元素 $pop-删除一个(1,表示删除最后一个。 -1表示删除第一个) $pushAll-添加一个或多个元素
> db.c5.update({}, {$push:{arr:[4,4,5]}}); > db.c5.find(); { "_id" : ObjectId("51e2421ecf05e15d315aa746"), "arr" : [ 1, 2, 3, [ 4, 4, 5 ] ] }
$addToSet 数组操作 - 添加无重复的元素
//添加一个
> db.c5.update({arr:{$in:[1,2,3]}}, {$addToSet:{arr:5}});
//配额 $each 添加多个
> db.c5.update({arr:{$in:[1,2,3]}}, {$addToSet:{arr:{$each:[5,6,7,8]}}});
$pull 针对数组操作 - 删除指定值。 $pullAll - 删除多个指定值
> db.c5.find() { "_id" : ObjectId("51e2421ecf05e15d315aa746"), "arr" : [ 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 0 ] } > db.c5.update({}, {$pull:{arr:4}}); > db.c5.find() { "_id" : ObjectId("51e2421ecf05e15d315aa746"), "arr" : [ 1, 2, 3, 5, 6, 7, 8, 9, 0 ] }
$rename 更改属性名
> db.c5.update({}, {$rename:{'arr':'post'}}); //字符串所以,必须打上引号 > db.c5.find() { "_id" : ObjectId("51e2421ecf05e15d315aa746"), "post" : [ 5, 6, 7, 8, 9, 0 ] }
$ 匹配前面 小json. 而并不是匹配整个json大数组
> db.c1.find() { "_id" : ObjectId("51e27de5cf05e15d315aa748"), "name" : "usr1", "arr" : [ { "title" : "linux" }, { "title" : "apache" }, { "content" : "i like it much" } ] } > db.c1.update({'arr.title':'linux'}, {$set:{'arr.$.cnt':'hahaha'}});//这里arr.title,arr....必须添加上引号 否则报错 > db.c1.find() { "_id" : ObjectId("51e27de5cf05e15d315aa748"), "arr" : [ { "cnt" : "hahaha", "title" : "linux" }, { "title" : "apache" }, { "content" : "i like it much" } ], "name" : "usr1" }
//find 游标。 //findOne 完完全全赋值出去 搭配save属性区别
> db.c1.insert({name:'user11', age:20}); > db.c1.find() { "_id" : ObjectId("51e280cfcf05e15d315aa749"), "name" : "user11", "age" : 20 } > var x = db.c1.findOne({name:'user11'});//不像find游标, 无法控制 > x { "_id" : ObjectId("51e280cfcf05e15d315aa749"), "name" : "user11", "age" : 20 } > x.sex = 'man' man > x.phone = '13889098909' 13889098909 > db.c1.save(x); > db.c1.find() { "_id" : ObjectId("51e280cfcf05e15d315aa749"), "name" : "user11", "age" : 20, "sex" : "man", "phone" : "13889098909" }