[MongoDB] Remove, update, create document

Remove:

remove the wand with the name of "Doom Bringer" from our wandscollection.

db.wands.remove({name: "Doom Bringer"})

>> WriteResult({'ngRemoved': 1})

When we removed the "Doom Bringer" wand, we noticed that it had a power of "Death", and we don't want any wands like that in our database. To be safe, let's remove any wands containing that in their powers.

db.wands.remove({name: "Doom Bringer", powers: "Death"})

Update:

Write the command to update the wand with a name of "Devotion Shift" and set the price to 5.99.

db.wands.update({name: "Devotion Shift"},{"$set": {price: 5.99}});

Update all the document: "multi"

Increase level_required by 2, apply to all the documents match {powers: "Fire"}:

db.wands.update(
  {powers: "Fire"},
  {"$inc":{level_required: 2}},
  {"multi": true}
)

Create new document if there is no existing one: "upsert"

db.logs.update(
  {name: "Dream Bender"},
  {"$inc": {count: 1}},
  {"upsert": true}
)
原文地址:https://www.cnblogs.com/Answer1215/p/5185369.html