[MongoDB] Remove, update, create document

本文演示了如何使用MongoDB进行数据库操作,包括移除特定名称的魔杖、删除包含特定力量的魔杖,以及更新魔杖的价格和批量增加魔法要求等级。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值