设为首页 加入收藏

TOP

mongodb的简单操作(二)
2015-11-21 01:52:03 来源: 作者: 【 】 浏览:1
Tags:mongodb 简单 操作
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”对应键值为正数则从尾部删除,为负数则从头部删除

?

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇去哪网实习总结:如何有效减少对.. 下一篇项目实战玩转数据库之上一篇下一篇

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: