mongodb 学习笔记六 Updating

本文详细介绍了MongoDB的原子更新操作,包括如何使用update方法更新文档、使用$操作符更新特定字段,以及upsert和multi参数的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文原文:http://www.mongodb.org/display/DOCS/Updating

mongodb支持原子的更新操作,对文档进行更新替换旧的文档或者更新文档中某个字段。

update操作用新的文档对象更新匹配的旧文档,如果仅希望更新文档中某些字段的值可以使用 “$"操作符,像后面提到的。

MongoDB shell syntax for update():

db.collection.update( criteria, objNew, upsert, multi )

Arguments:

  • criteria - query which selects the record to update;
  • objNew - updated object or $ operators (e.g., $inc) which manipulate the object
  • upsert - if this should be an "upsert" operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
  • multi - if all documents matching criteria should be updated
注意:一般情况下只更新匹配的第一个文档对象,如果想更新所有的匹配的文档对象,multi 选项必须为真。

           upsert 选项为真的时候,做更新操作或插入操作,即如果存在则更新,否则直接插入文档。

save()操作相当于update()操作在选项upsert为true的情况下,如下:

// x is some JSON style object
db.mycollection.save(x); // updates if exists; inserts if new

呵呵,今天就到这了,有时间再补充



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值