test.com" ] } 可见,删除了指定的数据项
?
7、$pop
$pop 跟 $pull 都是数组修改器,但是 $pop 只是从数组的头或者尾删除数组中的元素
先插入几个数据
?
> db.contact.update({"createtime":1},{"$push":{"phone":{"number":"12345678914","
place":"chengdu"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.contact.update({"createtime":1},{"$push":{"phone":{"number":"12345678915","
place":"chengdu"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.contact.update({"createtime":1},{"$push":{"phone":{"number":"12345678916","
place":"chengdu"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.contact.update({"createtime":1},{"$push":{"phone":{"number":"12345678917","
place":"chengdu"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 此时
?
?
> db.contact.find()
{ "_id" : ObjectId("55dac281081c67f0a7549fee"), "phone" : [ { "number" : "123456
78912", "place" : "chengdu" }, { "number" : "12345678913", "place" : "chengdu" }
, { "number" : "12345678914", "place" : "chengdu" }, { "number" : "12345678915",
"place" : "chengdu" }, { "number" : "12345678916", "place" : "chengdu" }, { "nu
mber" : "12345678917", "place" : "chengdu" } ], "createtime" : 1, "email" : [ "1
23456789@test.com" ] } 执行
?
?
> db.contact.update({"createtime":1},{"$pop":{"phone":-1}}) 得到
?
?
> db.contact.find()
{ "_id" : ObjectId("55dac281081c67f0a7549fee"), "phone" : [ { "number" : "123456
78913", "place" : "chengdu" }, { "number" : "12345678914", "place" : "chengdu" }
, { "number" : "12345678915", "place" : "chengdu" }, { "number" : "12345678916",
"place" : "chengdu" }, { "number" : "12345678917", "place" : "chengdu" } ], "cr
eatetime" : 1, "email" : [ "123456789@test.com" ] } 操作结果,从头部删除了数据项
?
再次执行
?
> db.contact.update({"createtime":1},{"$pop":{"phone":1}}) 得到
?
?
> db.contact.find()
{ "_id" : ObjectId("55dac281081c67f0a7549fee"), "phone" : [ { "number" : "123456
78913", "place" : "chengdu" }, { "number" : "12345678914", "place" : "chengdu" }
, { "number" : "12345678915", "place" : "chengdu" }, { "number" : "12345678916",
"place" : "chengdu" } ], "createtime" : 1, "email" : [ "123456789@test.com" ] } 操作结果,从尾部删除了数据项
其实,“phone”对应键值为正数则从尾部删除,为负数则从头部删除
?