1.update
db.user.update({name:"chengcheng"},{$set:{"name":"jiangwei"}})
如果不加$set,会将那条数据完全替换
db.user.update({name:"jiangwei"},{name:"huan"}})
2.remove删除某条数据
db.collectionName.remove(condition)
db.user.remove({name:'li'})
2.1justOne:true只删除一条数据
db.user.remove({name:"shanshan"},{justOne:true})
3.更新内嵌数组中某条数据
//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}})