1.update

  1. db.user.update({name:"chengcheng"},{$set:{"name":"jiangwei"}})

如果不加$set,会将那条数据完全替换

  1. db.user.update({name:"jiangwei"},{name:"huan"}})

2.remove删除某条数据

  1. db.collectionName.remove(condition)
  1. db.user.remove({name:'li'})

2.1justOne:true只删除一条数据

  1. db.user.remove({name:"shanshan"},{justOne:true})

3.更新内嵌数组中某条数据

  1. //user
  2. //更新cartList中id等于1002这条数据,将num设置为2
  3. {
  4. _id: 4,
  5. cartList: [
  6. { id: 1001, num: 1, name: "手机" },
  7. { id: 1002, num: 1, name: "电脑" },
  8. { id: 1003, num: 1, name: "平板" }
  9. ]
  10. }
  1. db.user.update({_id:4,"cartList.id":1001},{$set:{"cartList.$.num":2}})