你的位置:首页 > 数据库类
更新记录
$set 用来指定一个键并更新键值,若键不存在并创建。
$unset 用来删除键
inc修改器inc可以对文档的某个值为数字型(只能为满足要求的数字)的键进行增减的操作
$push 向文档的某个数组类型的键添加一个数组元素,不过滤重复的数据。添加时键存在,要求键值类型必须是数组;键不存在,则创建数组类型的键
$pushAll 指定多个
$addToSet 往一个数组里插入记录
$pop 删除数组内的一个值
$pull 从数组field内删除一个等于value值
pullAll同pull,可以一次删除数组内的多个值
$rename 修改键值名称
$mul 乘
$min
$max
$currentDate
> var single=db.zxq.findOne();
> single
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "zxq" }
> single.name="zxq3"
zxq3
> db.zxq.update({name:"zxq"},single);
> db.zxq.find();
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2, "name" : "zxq1" }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "zxq3" }
//update更新:
db.zxq.update(parm1,parm2,parm3,parm4)
parm1:需要更新的条件
parm2:更新的对象
parm3:如果没有符合条件的对象,是否新增一条记录,1(新增),默认值为0
parm4:如果有多个符合条件的记录,是否全部更新,默认0,如果全部更新取值1
语法:
db.zxq.update({name:"a"},{$set:{age:20}},0,1) ---更新age键值为20
db.zxq.update({name:"a"},{$unset:{age:1}},1,1) ---删除age键
db.zxq.update({name:"a"},{$inc:{age:1}},0,1) ---age数据加1
db.zxq.update({name:"a"},{$inc:{age:-1}},0,1) ---age数据减1
例如:set[语法:db.collection.update( { field: value1 }, {set: { field1: value2 } } );]
> db.zxq.update({name:"a"},{$set:{age:20}},0,1) //0表示没有符合条件的不进行增加
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2, "name" : "zxq1" }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "zxq3" }
> db.zxq.update({name:"a"},{$set:{age:20}},1,1) //1表示没有符合条件的进行增加1行
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2, "name" : "zxq1" }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "zxq3" }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
>$unset
> db.zxq.update({id:1},{$unset:{name:1}},0,1)
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }$inc
> db.zxq.update({name:"zxqzxq"},{$inc:{id:1}},0,1) //0表示没有符合条件的不进行增加
>
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
> db.zxq.update({name:"zxqzxq"},{$inc:{id:1}},1,1) //1表示没有符合条件的进行增加
>
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 1, "name" : "zxqzxq" }
>
> db.zxq.update({name:"zxqzxq"},{$inc:{id:1}},1,1) //有符合条件的id:1进行增加1
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 2, "name" : "zxqzxq" }
upsert 参数的更新与parm4=1效果一样,在没有符合条件的情况下增加一条记录
> db.zxq.find({id:6})
> db.zxq.update({id:6},{$set:{name:"xxxx"}})
> db.zxq.find({id:6})
> db.zxq.update({id:6},{$set:{name:"xxxx"}},{upsert:true})
> db.zxq.find({id:6})
{ "_id" : ObjectId("57c1cebad7f41e4ad2dad410"), "id" : 6, "name" : "xxxx" }
> db.zxq.update({name:"zxqzxq"},{$inc:{id:2}},1,1) //有符合条件的id:2进行增加2
>
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 4, "name" : "zxqzxq" }
> db.zxq.update({name:"zxqzxq"},{$inc:{id:-2}}) //有符合条件的id:-2进行减少2
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 4, "name" : "zxqzxq" }
同时更新多条记录
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1 }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 1, "name" : "zxqzxq" }
//准备更新id:1的所有记录
> db.zxq.update({id:1},{$set:{name:"zxq"}})
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "zxq" } //被更新了,增加了键值
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 1, "name" : "zxqzxq" } //未被更新
//发现只更新了1条记录
> db.zxq.update({id:1},{$set:{name:"aaa"}},0,1) //1表示,如果存在多条,则同时被更新
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "aaa" }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 1, "name" : "aaa" }
//{multi:true}同样达到更新多条目的
> db.zxq.update({id:1},{$set:{name:"xxxx"}},{multi:true})
> db.zxq.find()
{ "_id" : ObjectId("57c1c4ea8ee7bef4894d54b2"), "id" : 2 }
{ "_id" : ObjectId("57c1c4368ee7bef4894d54b1"), "id" : 1, "name" : "xxxx" }
{ "_id" : ObjectId("57c1c77cd7f41e4ad2dad40d"), "age" : 20, "name" : "a" }
{ "_id" : ObjectId("57c1c843d7f41e4ad2dad40e"), "id" : 3, "name" : "aa" }
{ "_id" : ObjectId("57c1c98ad7f41e4ad2dad40f"), "id" : 1, "name" : "xxxx" }$push
> db.a.find()
{ "_id" : ObjectId("57c1d3f155a4d399ca642249"), "id" : 1, "size" : "a" }
{ "_id" : ObjectId("57c1d3f555a4d399ca64224a"), "id" : 1, "size" : { "a" : 1, "b" : 2 } }
{ "_id" : ObjectId("57c1d2b7be0a1dfd5217d814"), "id" : 1, "name" : "a", "size" : { "c" : [ 3 ] } }
{ "_id" : ObjectId("57c1d4c321b1ed73ff5c2968"), "id" : 2, "size" : { "c" : [ 3 ] } }
>
> db.a.update({id:2},{$push:{"size.a":1}})
> db.a.find()
{ "_id" : ObjectId("57c1d3f155a4d399ca642249"), "id" : 1, "size" : "a" }
{ "_id" : ObjectId("57c1d3f555a4d399ca64224a"), "id" : 1, "size" : { "a" : 1, "b" : 2 } }
{ "_id" : ObjectId("57c1d2b7be0a1dfd5217d814"), "id" : 1, "name" : "a", "size" : { "c" : [ 3 ] } }
{ "_id" : ObjectId("57c1d4c321b1ed73ff5c2968"), "id" : 2, "size" : { "a" : [ 1 ], "c" : [ 3 ] } } //数组类型的键添加一个数组元素
> db.a.find({id:1})
{ "_id" : ObjectId("57c1d3f155a4d399ca642249"), "id" : 1, "size" : "a" }
{ "_id" : ObjectId("57c1d3f555a4d399ca64224a"), "id" : 1, "size" : { "a" : 1, "b" : 2 } }
{ "_id" : ObjectId("57c1d2b7be0a1dfd5217d814"), "id" : 1, "name" : "a", "size" : { "c" : [ 3 ] } } //向数组中增加元素
> db.a.update({id:1,name:"a"},{$push:{"size.d":4}},1,0)
> db.a.find({id:1})
{ "_id" : ObjectId("57c1d3f155a4d399ca642249"), "id" : 1, "size" : "a" }
{ "_id" : ObjectId("57c1d3f555a4d399ca64224a"), "id" : 1, "size" : { "a" : 1, "b" : 2 } }
{ "_id" : ObjectId("57c1d2b7be0a1dfd5217d814"), "id" : 1, "name" : "a", "size" : { "c" : [ 3 ], "d" : [ 4 ] } }pushAll [ 语法:{pushAll: { : [ , , … ] } } ]指定多个值
> db.a.update({id:1,name:"a"},{$pushAll:{"size.e":[5,6,7]}})
> db.a.find({id:1})
{ "_id" : ObjectId("57c1d3f155a4d399ca642249"), "id" : 1, "size" : "a" }
{ "_id" : ObjectId("57c1d3f555a4d399ca64224a"), "id" : 1, "size" : { "a" : 1, "b" : 2 } }
{ "_id" : ObjectId("57c1d2b7be0a1dfd5217d814"), "id" : 1, "name" : "a", "size" : { "c" : [ 3 ], "d" : [ 4 ], "e" : [ 5, 6, 7 ] } }
>addToSet 语法:{addToSet: { : , … } }
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 10, 20 ] }
> db.a.update({ id:1 },{ $addToSet: {size: [ 30,40] } })
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 10, 20, [ 30, 40 ] ] }
>addToSet和each 是往一个数组里插入每个记录,并保证元素唯一
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1 }
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 1, 2, 1]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 1, 2 ] }
//测试数组操作
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2, 3, 4, 5, 6, 7 ] ] }
> db.a.update({id : 1}, {$addToSet : {size : [1, 2, 1, 2, 3,4,5,6,7]}})
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2, 3, 4, 5, 6, 7 ] ] }
> db.a.update({id : 1}, {$addToSet : {size : [1, 2, 1, 2, 3,4,5,6,7,8]}})
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2, 3, 4, 5, 6, 7 ],
[ 1, 2, 1, 2, 3, 4, 5, 6, 7, 8 ] ] }
>
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1 }
> db.a.update({id : 1}, {$addToSet : {size : [1, 2, 1, 2]}})
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2 ] ] }
>
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 1, 2, 1]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2 ], 1, 2 ] }
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 1, 2, 1]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2 ], 1, 2 ] }
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 1, 2, 1,3,4,5]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ [ 1, 2, 1, 2 ], 1, 2, 3, 4, 5 ] }
>
//测试数组操作
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 3,4]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 1, 2, 3, 4 ] }
> db.a.update({id : 1}, {$addToSet : {size : {$each : [ 1, 2, 3,4,5,6,7]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 1, 2, 3, 4, 5, 6, 7 ] }
> db.a.update({id : 1}, {$addToSet : {size : {$each : [8,9,10]}}}, true)
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }$pop 只能删除一个值,也就是说只能用1或-1
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 1, 2 ] }
> db.a.update( { id:1} , { $pop : { "size": -1 } } );
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 2 ] }
>
> db.a.update( { id:1} , { $pop : { "size": -1 } } ); //-1从前面删除1个值
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }
> db.a.update( { id:1} , { $pop : { "size": 1 } } ); //1从后面删除1个值
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 2, 3, 4, 5, 6, 7, 8, 9 ] }
> db.a.update( { id:1} , { $pop : { "size": 2 } } ); //其它值和1或-1效果一样
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 2, 3, 4, 5, 6, 7, 8 ] }
> db.a.update( { id:1} , { $pop : { "size": -2 } } ); //其它值和1或-1效果一样
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 3, 4, 5, 6, 7, 8 ] }$pull
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 4, 5, 6, 7, 8 ] }
> db.a.update( { "id" : 1 } , { $pull : { "size":6} } );
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 4, 5, 7, 8 ] }
>$pullAll
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 4, 5, 7, 8 ] }
> db.a.update({id:1},{$pull:{size:[4,8]}});
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 4, 5, 7, 8 ] }
> db.a.update({id:1},{$pullAll:{size:[4,8]}});
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 5, 7 ] }
>rename 语法:{rename: { : , : , … } }
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size1" : [ 5, 7 ] }
> db.a.update({id:1},{$rename:{"size1":"size"}});
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 5, 7 ] }
>
> db.a.insert({id:2,"name": { "first" : "george", "last" : "washington" }})
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 5, 7 ] }
{ "_id" : ObjectId("57c27c713b5e3ba4c68d0d78"), "id" : 1 }
{ "_id" : ObjectId("57c27c963b5e3ba4c68d0d79"), "id" : 2, "name" : { "first" : "george", "last" : "washington" } }
> db.a.update({id:2}, {$rename: { "name.first": "name.fname"}})
> db.a.find()
{ "_id" : ObjectId("57c273813b5e3ba4c68d0d77"), "id" : 1, "size" : [ 5, 7 ] }
{ "_id" : ObjectId("57c27c713b5e3ba4c68d0d78"), "id" : 1 }
{ "_id" : ObjectId("57c27c963b5e3ba4c68d0d79"), "id" : 2, "name" : { "fname" : "george", "last" : "washington" } }$mul
{ _id: 1, item: "ABC", price: 10.99 }
db.zxq.update(
{ _id: 1 },
{ $mul: { price: 1.25 } }
)
{ _id: 1, item: "ABC", price: 13.7375 }$min
> db.zxq.insert({ _id: 1, highScore: 800, lowScore: 200 })
WriteResult({ "nInserted" : 1 })
> db.zxq.find()
{ "_id" : 1, "highScore" : 800, "lowScore" : 200 }
> db.zxq.update( { _id: 1 }, { $min: { lowScore: 150 } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.zxq.find()
{ "_id" : 1, "highScore" : 800, "lowScore" : 150 }
> db.zxq.update( { _id: 1 }, { $min: { lowScore: 250 } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.zxq.find()
{ "_id" : 1, "highScore" : 800, "lowScore" : 150 }
>$max
> db.zxq.find()
{ "_id" : 1, "highScore" : 800, "lowScore" : 150 }
> db.zxq.update( { _id: 1 }, { $max: { highScore: 950 } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.zxq.find()
{ "_id" : 1, "highScore" : 950, "lowScore" : 150 }
> db.zxq.update( { _id: 1 }, { $max: { highScore: 870 } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.zxq.find()
{ "_id" : 1, "highScore" : 950, "lowScore" : 150 }
> db.a.insert({_id: 1,desc: "crafts",dateEntered: ISODate("2013-10-01 05:00:00"),dateExpired: ISODate("2013-10-01 16:38:16.16")})
WriteResult({ "nInserted" : 1 })
> db.a.find()
{ "_id" : 1, "desc" : "crafts", "dateEntered" : ISODate("2013-10-01T05:00:00Z"), "dateExpired" : ISODate("2013-10-01T16:38:16.160Z") }
> db.a.update({ _id: 1 },{ $max: { dateExpired: new Date("2013-09-30") } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.a.find()
{ "_id" : 1, "desc" : "crafts", "dateEntered" : ISODate("2013-10-01T05:00:00Z"), "dateExpired" : ISODate("2013-10-01T16:38:16.160Z") }
>$currentDate
> db.b.insert({ _id: 1, status: "a", lastModified: ISODate("2013-10-02 01:11:18") })
WriteResult({ "nInserted" : 1 })
> db.b.find()
{ "_id" : 1, "status" : "a", "lastModified" : ISODate("2013-10-02T01:11:18Z") }
>
> db.b.update({ _id: 1 }, { $currentDate: { lastModified: true, "cancellation.date": { $type: "timestamp" } },
$set: { status: "D", "cancellation.reason": "user request" }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.b.find()
{ "_id" : 1, "status" : "D", "lastModified" : ISODate("2016-08-28T06:55:58.745Z"), "cancellation" :
{ "date" : Timestamp(1472367358, 1), "reason" : "user request" } }
>
> db.b.find()
{ "_id" : 2, "lastModified" : ISODate("2016-08-28T08:47:21.837Z") }
> db.b.update({ _id:1}, { $currentDate: { lastModified: true}},1,1)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
> db.b.find()
{ "_id" : 2, "lastModified" : ISODate("2016-08-28T08:47:21.837Z") }
{ "_id" : 1, "lastModified" : ISODate("2016-08-28T08:48:01.274Z") }
>七、索引,执行计划
for (var i=0;i<10000;i++){db.ccc.insert({"name":"a"+i,"age":i+1})}
> db.ccc.count()
214069
> db.ccc.find({age:100})
{ "_id" : ObjectId("57c2e2c56da14df0c2b1cc85"), "name" : "a99", "age" : 100 }创建B-tree索引
> db.ccc.ensureIndex({age:1})
> db.ccc.find({age:100}).explain()
{
"cursor" : "BtreeCursor age_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"age" : [
[
100,
100
]
]
},
"server" : "linux:27017"
}创建符合索引 (1:代表升序, -1:代表降序)
> db.ccc.ensureIndex({age:1,name:1})
db.ccc.ensureIndex({"age":1,"name":-1})
> db.ccc.find({name:"a0"})
{ "_id" : ObjectId("57c2e2c56da14df0c2b1cc22"), "name" : "a0", "age" : 1 }
> db.ccc.find({name:"a0"}).explain()
{
"cursor" : "BtreeCursor name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"a0",
"a0"
]
]
},
"server" : "linux:27017"
}唯一索引
> db.ccc.ensureIndex({"age":1},{"unique":true})
>查看索引
> db.ccc.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.ccc",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"age" : 1
},
"ns" : "test.ccc",
"name" : "age_1"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "test.ccc",
"name" : "name_1"
},
{
"v" : 1,
"key" : {
"age" : 1,
"name" : 1
},
"ns" : "test.ccc",
"name" : "age_1_name_1"
}
]
>
> db.system.indexes.find() //查看所有索引
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.zxq", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.a", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.users", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.ccc", "name" : "_id_" }
{ "v" : 1, "key" : { "age" : 1 }, "ns" : "test.ccc", "name" : "age_1" }
{ "v" : 1, "key" : { "name" : 1 }, "ns" : "test.ccc", "name" : "name_1" }
{ "v" : 1, "key" : { "age" : 1, "name" : 1 }, "ns" : "test.ccc", "name" : "age_1_name_1" }
>删除索引
删除集合所有索引
db.ccc.dropIndexes();
删除特定索引 (删除id字段升序的索引)
db.ccc.dropIndex({"id":1})
> db.ccc.dropIndex({"age" : 1})
{ "nIndexesWas" : 2, "ok" : 1 }
查看索引:
db.ccc.getIndexes()
db.system.indexes.find()
重构索引(慎用)
db.articles.reIndex();
注意:索引排序规则升序:1,降序-1八、聚合函数
count
> db.zxq.count() 1000
distinct
> db.runCommand({"distinct":"zxq","key":"name"})
"a997",
"a998",
"a999",
"a1000"
],
"stats" : {
"n" : 1000,
"nscanned" : 1000,
"nscannedObjects" : 1000,
"timems" : 2,
"cursor" : "BasicCursor"
},
"ok" : 1
}
distinct使用
db.xxx.distinct("age")
> db.zzz.distinct("id")
[ 1, 2, 3, 4, 5 ]group
select a,b,sum(c) csum from coll where active=1 group by a,b;
db.coll.group({
'key':{
'a':true,
'b':true
},
'cond':{ 'active':1 },
'reduce': function(obj, prev)
{
prev.csum ++
},
'initial':{'csum': 0}
})
db.zxq.group({
'key':{
'id':true,
'name':true
},
'cond':{ 'id':1 }, //cond用于筛选条件
'reduce': function(obj, prev) //obj用于扫描行,prev用于输出结果
{
prev.csum ++
},
'initial':{'csum': 0} //设定初始值
})
//如下案例
> db.zxq.find({id:1})
{ "_id" : ObjectId("57cbbbe7c2993ede6eb626f6"), "id" : 1, "name" : "a1" }
{ "_id" : ObjectId("57cbc9a0c2993ede6eb62ade"), "id" : 1, "name" : "a1" }
{ "_id" : ObjectId("57cbc9a2c2993ede6eb62ec6"), "id" : 1, "name" : "a1" }
{ "_id" : ObjectId("57cbc9a5c2993ede6eb632ae"), "id" : 1, "name" : "a1" }
> db.zxq.group({ 'key':{ 'id':true,'name':true },'cond':{'id':1},'initial':{'csum': 0},'reduce': function(obj, prev) { prev.csum ++ } })
[ { "id" : 1000, "name" : "a1000", "csum" : 4 } ]
//写法二,runCommand
db.runCommand({group:
{
ns:"zxq",
key:{id:true,name:true},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++}
}
});
//添加筛选条件
db.zxq.group({key:{id:true,name:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
condition:{id:{$gt:990}}
});
//写法二,添加筛选条件
db.runCommand({group:
{
ns:"zxq",
key:{id:true,name:true},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++},
condition:{id:{$gt:998}}
}
});
//使用函数返回值分组
> db.zxq.group({$keyf:function(doc){return {name:doc.name};},initial:{num:0},$reduce:function(doc,prev){ prev.num++ }, condition:{id:{$gt:997}} });
[
{
"name" : "a998",
"num" : 4
},
{
"name" : "a999",
"num" : 4
},
{
"name" : "a1000",
"num" : 4
}
]
>
//写法二
db.runCommand({group:
{
ns:"zxq",
$keyf:function(doc){return {name:doc.name};},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++}
}
});
//使用终结器
db.zxq.group({$keyf:function(doc){return {name:doc.name};},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},condition:{id:{$gt:997}},
finalize: function(doc){ doc.count=doc.num;delete doc.num; }
});
[
{
"name" : "a998",
"count" : 4
},
{
"name" : "a999",
"count" : 4
},
{
"name" : "a1000",
"count" : 4
}
]
>
//写法二
db.runCommand({group:
{
ns:"zxq",
$keyf:function(doc){return {name:doc.name};},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++},condition:{id:{$gt:997}},
finalize: function(doc){ doc.count=doc.num;delete doc.num; }
}
});
{
"retval" : [
{
"name" : "a998",
"count" : 4
},
{
"name" : "a999",
"count" : 4
},
{
"name" : "a1000",
"count" : 4
}
],
"count" : 12,
"keys" : 3,
"ok" : 1
}未完待续......
- 上一篇:MongoDB入门学习(二)
- 下一篇:MongoDB入门学习(四)

- 发表评论
- 查看评论
【暂无评论!】发表评论: