MongoDB update修改器: 针对Arrays的$修改器 $push $pull $pop

 

 

针对Arrays的$修改器

 $push :

 

  { $push: { key: value } }

 

 

 

它是用来对Array (list)数据类型进行 增加 新元素的,相当于我们Python中 list.append() 方法

做一个小例子 :首先我们要先对原有数据增加一个Array类型的field:

> db.user.updateOne({age:34},{$unset:{hobby:1} })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34 }

对age=34的数据添加hobby字段 列表

 

> db.user.updateOne({age:34},{$set:{hobby:["羽毛球","篮球","足球"]} })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }

对age=34的数据的hobby列表添加一个桌球数据

> db.user.updateOne({age:34},{$push:{hobby:'桌球' } } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球", "桌球" ] }

$push 是在 Array(list) 的尾端加入一个新的元素  {$push : {"hobby" : "桌球"}}

 

$pull:

 

 { $pull: { key: value } }

 

 

 

 有了$push 对Array列表类型进行增加,就一定有办法对其内部进行删减,$pull 就是指定删除Array列表中的某一个元素

例子:

把age=34的数据的hobby列表里面桌球元素删除

> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球", "桌球" ] }

> db.user.updateOne({age:34},{$pull:{hobby:'桌球' } } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }

 

如果Array列表里面有多个桌球相同元素会怎么样?

添加两个桌球元素

> db.user.updateOne({age:34},{$push:{hobby:'桌球'} } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球", "桌球" ] }

> db.user.updateOne({age:34},{$push:{hobby:'桌球'} } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球", "桌球", "桌球" ] }
 

 

把列表所有"桌球"的元素都删除掉

> db.user.updateOne({age:34},{$pull:{hobby:'桌球' } } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }


得出了一个结论,只要满足条件,就会将Array列表中所有满足条件的数据全部清除掉

 

 

修改列表里面元素的名字

例子:

修改age=34的数据,hobby里面的"足球"改为"网球"

> db.user.updateOne({age:34,hobby:"足球"},{$set:{"hobby.$":"网球" } } )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "网球" ] }

 了解MongoDB "$" 可以看这个帖子 MongoDB "$" 字符 下标位置

 

$pop:

指定删除Array列表中的第一个 或 最后一个 元素

-1 代表第一个元素, 1 代表最后元素

> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }
> 

删除age=34 hobby 列表 第一个元素

> db.user.updateOne({age:34},{$pop:{hobby:-1}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "篮球", "足球" ] }

 

删除age=34 hobby 列表 最后一个元素

> db.user.updateOne({age:34},{$pop:{hobby:1}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({age:34})
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "篮球" ] }

 

posted @ 2019-04-13 15:58  minger_lcm  阅读(879)  评论(0编辑  收藏  举报