码迷,mamicode.com
首页 > 数据库 > 详细

[MongoDB] Remove, update, create document

时间:2016-02-09 01:22:07      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

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}
)

[MongoDB] Remove, update, create document

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5185369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!