数组的定位修改器
-------------------------------------------------------------------
在需要对数组中的值进行操作的时候,可通过位置或者定位操作符("$").数组是0开始的,可以直接将下标作为键来选择元素。
查询全部
>db.foo.find()
{"_id":ObjectId("544a3ddad4646f0c8c904965"),"a":4}
{"_id":ObjectId("544a3dc0d4646f0c8c904962"),"a":7,"name":"zhangsan2"}
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"uid":"001","comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}]}
{"_id":ObjectId("5463179ea14bafe4547cafc1"),"a":9,"uid":"001","comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}]}
查询a=8
>db.foo.find({a:8})
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"uid":"001","comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}]}
更新数据中第一个元素
>db.foo.update({a:8},{$set:{"comments.0.name":"t11"}}) 当更新第一个元素时,可使用$,即 comments.$.name
>db.foo.find({a:8})
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"comments":[{"name":"t11","size":10},{"name":"t2","size":12},{"name":"t5","size":14}],"uid":"001"}
更新数据中第二个元素
>db.foo.update({a:8},{$set:{"comments.1.name":"t22"}})
>db.foo.find({a:8})
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"comments":[{"name":"t11","size":10},{"name":"t22","size":12},{"name":"t5","size":14}],"uid":"001"}
>db.foo.find()
{"_id":ObjectId("544a3ddad4646f0c8c904965"),"a":4}
{"_id":ObjectId("544a3dc0d4646f0c8c904962"),"a":7,"name":"zhangsan2"}
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}],"uid":"001"
}
{"_id":ObjectId("5463179ea14bafe4547cafc1"),"a":9,"uid":"001","comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}]}
>db.foo.update({$set:{"comments.1.name":"t22"}})
assertfailed:needanobject -——提示只能更新一个对象中的数据元素
Error:PrintingStackTrace
atprintStackTrace(src/mongo/shell/utils.js:37:15)
atdoassert(src/mongo/shell/assert.js:6:5)
atassert(src/mongo/shell/assert.js:14:5)
atDBCollection.update(src/mongo/shell/collection.js:220:5)
at(shell):1:8
WedNov1216:25:52.234assertfailed:needanobjectatsrc/mongo/shell/assert.js:7
>db.foo.update({},{$set:{"comments.1.name":"t22"}}) --默认更新一条记录,这种方式虽然不报错,但是不对
>db.foo.find({})
{"_id":ObjectId("544a3dc0d4646f0c8c904962"),"a":7,"name":"zhangsan2"}
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}],"uid":"001"}
{"_id":ObjectId("5463179ea14bafe4547cafc1"),"a":9,"uid":"001","comments":[{"name":"t1","size":10},{"name":"t2","size":12},{"name":"t5","size":14}]}
{"_id":ObjectId("544a3ddad4646f0c8c904965"),"a":4,"comments":{"1":{"name":"t22"}}}
>db.foo.update({},{$set:{"comments.1.name":"t222"}},0,1)--更全部的查询记录,这种方式也不报错,但是也不对的
>db.foo.find({})
{"_id":ObjectId("5463178aa14bafe4547cafc0"),"a":8,"comments":[{"name":"t1","size":10},{"name":"t222","size":12},{"name":"t5","size":14}],"uid":"001"}
{"_id":ObjectId("5463179ea14bafe4547cafc1"),"a":9,"comments":[{"name":"t1","size":10},{"name":"t222","size":12},{"name":"t5","size":14}],"uid":"001"}
{"_id":ObjectId("544a3ddad4646f0c8c904965"),"a":4,"comments":{"1":{"name":"t222"}}}
{"_id":ObjectId("544a3dc0d4646f0c8c904962"),"a":7,"comments":{"1":{"name":"t222"}},"name":"zhangsan2"}
>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4 }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan" }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2" }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [ 2, 3, 4, 5 ] }
> db.foo.update({a:3},{$push:{title:6}})
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4 }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan" }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2" }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [ 2, 3, 4, 5, 6 ] }
> db.foo.update({},{$push:{title:6}},0,1)
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4, "title" : [ 6 ] }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan", "title" : [ 6 ] }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2", "title" : [ 6 ] }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [ 2, 3, 4, 5, 6, 6 ] }
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4, "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [2,3,4,5,6,6,6 ] }
> db.foo.update({a:3},{$set:{"title.$":22}})
Cannot apply the positional operator without a corresponding query field containing an array.
> db.foo.update({a:3},{$set:{"title.0":22}})
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4, "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [22,3,4,5,6,6,6 ] }
> db.foo.update({a:3},{$set:{"title.1":33}})
> db.foo.find()
{ "_id" : ObjectId("544a3ddad4646f0c8c904965"), "a" : 4, "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd0d4646f0c8c904963"), "a" : "2.3", "name" : "zhangsan", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dc0d4646f0c8c904962"), "a" : 7, "name" : "zhangsan2", "title" : [6,6 ] }
{ "_id" : ObjectId("544a3dd5d4646f0c8c904964"), "a" : 3, "title" : [22,33,4,5,6,6,6 ] }
>