1. 程式人生 > >MongoDB update修改器: 針對Arrays的$修改器 $push $pull $pop

MongoDB update修改器: 針對Arrays的$修改器 $push $pull $pop

col color 類型 全部 nbsp knowledge fin pen strong

針對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" : [ "籃球" ] }

MongoDB update修改器: 針對Arrays的$修改器 $push $pull $pop