一、$pull删除数组中符合条件的值
//stores{id:1000, cartList:[ {productId:1001,name:'手机'}, {productId:1002,name:"电脑"}, {productId:1003,name:"平板"} ]}
//删除手机这条数据db.stores.update({id:1000},{$pull:{"cartList":{"productId":1001}}})
二、$push向数组中添加一条数据
db.stores.update({id:1000},{$push:{"cartList":{productId:1004,name:"电动车"}}})
三、更新内嵌数组中某条数据
//user//更新cartList中id等于1002这条数据,将num设置为2{ _id: 4, cartList: [ { id: 1001, num: 1, name: "手机" }, { id: 1002, num: 1, name: "电脑" }, { id: 1003, num: 1, name: "平板" } ]}
db.user.update({_id:4,"cartList.id":1001},{$set:{"cartList.$.num":2}})