在MongoDB中,更新单个doc的操作是原子性的。默认情况下,如果一个update操作更新多个doc,那么对每个doc的更新是原子性的,但是对整个update 操作而言,不是原子性的,可能存在前面的doc更新成功,而后面的doc更新失败的情况。由于更新单个doc的操作是原子性的,如果两个更新同时发生,那么一个更新操作会阻塞另外一个,doc的最终结果值是由时间靠后的更新操作决定的。

通过使用 $isolated option,能够确保更新多个doc的写操作是原子性的,任何查询操作都会读取到更新操作,直到该操作完成(成功或失败)。

Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written. By using the $isolatedoption, you can ensure that no client sees the changes until the operation completes or errors out.

MongoDB在新增和更新数据的时候,不会实时写入到Disk中,可能会丢失数据。

一,语法

默认情况下,update只会更新single doc,如果需要更新多个doc,必须显式设置doc: multi:true。

db.collection.update(<query>,<update>,{upsert: <boolean>,multi: <boolean>,writeConcern: <document>}
)

upsert:Optional. If set to true, creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found.

multi:Optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.

二,更新示例

在users collection中有三个doc,插入代码如下

user1={ name:"t1", age:21}
user2={ name:"t2", age:22}
user3={ name:"t3", age:23}db.users.insert([user1,user2,user3])

1,upsert option usage

upsert option的含义是,如果collection中存在匹配的doc,那么更新该doc;如果不匹配任何doc,那么插入一条新的doc。

使用update命令和 upsert选项,插入一条新的user,name=“t4”

db.users.update({name:"t4"},{name:"t4"},{upsert:true})

对新插入的user新增field:age=24,必须将doc的所有field都显式指定。

db.users.update({age:24},{name:"t4",age:24})

如果在Update doc中,只包含age=24,那么将失去name field,例如

 db.users.update({name:"t4"},{age:24})

2,multi option usage

在使用multi option更新多个doc之前,先考虑一个问题,如果把age的年纪都加1,那么在age加1时,保持其他field不变。这种情况需要用到$inc operator,用于将指定字段的值递增,同时不会影响其他字段。

{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

示例,将符合条件的User的age 加 1

db.users.update({age:{$lt:24}},{$inc:{age:1}},{multi:true})

3,为所有的user 增加字段

这种scenario需要用到$set operator,用于替换指定字段的值,或新增字段。

{ $set: { <field1>: <value1>, ... } }

The $set operator replaces the value of a field with the specified value. If the field does not exist, $set will add a new field with the specified value, provided that the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, $set will create the embedded documents as needed to fulfill the dotted path to the field.

示例,为所有的user增加sex字段,默认值是femal。

db.users.update({},{$set:{sex:"femal"}},{multi:true})

三,原子更新multiple doc

在query filter中加入 $isolated:1,表示对于查询到的所有doc,update操作将会在一个原子操作中完成。

db.users.update({$isolated:1},{$set:{sex:"femal"}},{multi:true})

四,更新doc的结构

1,将doc的sex field删除

Step1,使用FindOne找到name=t4的User

t4=db.users.findOne({name:"t4"})

Step2,使用delete command 删除sex field

delete t4.sex;

Step3,使用Updae 替换原来的doc

 db.users.update({name:"t4"},t4);

step4,使用find 查看doc的数据更新

2,使用db.collection.replaceOne替换doc

db.collection.replaceOne(<filter>,<replacement>,{upsert: <boolean>,writeConcern: <document>}
)

step1,使用findOne()查找一个doc

 t4=db.users.findOne({name:"t4"})

step2,为doc增加一个sex field

t4.sex="femal"

step3,使用replaceOne函数替换doc

db.users.replaceOne({name:"t4"},t4)

3,使用$set对所有符合query filter的doc批量修改doc结构

db.users.update({age:{$lte:23,$gte:21}},{$set:{sex:"femal"}},{multi:true});

4,使用$unset对所有符合query filter的doc批量删除doc的field

db.users.update({$and:[{age:{$lte:23}},{age:{$gte:21}}]},{$unset:{sex:"femal"}},{multi:true})--or
db.users.update({age:{$lte:23,$gte:21}},{$set:{sex:"femal"}},{multi:true});

参考doc:

Update Documents

Atomicity and Transactions

$isolated

$set

MongoDB 文档的更新操作相关推荐

  1. 【三】MongoDB文档的CURD操作

    一.插入文档 使用insert方法插入文档到一个集合中,如果集合不存在创建集合,有以下几种方法: db.collection.insertOne({}):(v3.2 new)  #插入一个文档到集合中 ...

  2. mongodb php代码实例,MongoDB文档的更新(php代码实例)

    MongoDB更新文档分为两大类: 文档替换,使用新文档完全替换掉旧文档 修改器,修改部分文档 文档替换 使用文档替换非常的简单,下面来看演示: $collect->insertOne(['na ...

  3. mongodb php update,MongoDB文档的更新(php代码实例)

    MongoDB更新文档分为两大类:文档替换,使用新文档完全替换掉旧文档 修改器,修改部分文档 文档替换 使用文档替换非常的简单,下面来看演示:$collect->insertOne(['name ...

  4. 腾讯文档-协同更新操作

    需求:有一个文档或者excel需要多个人运维,且可以同时进行增删改查的操作,并导出可视化功能! http://www.dpriver.com/pp/sqlformat.htm?ref=wangz.sq ...

  5. MongoDB——文档操作(更新文档)

    目录 一.更新文档命令的格式 二.更新操作符 三.更新单个文档 3.1.更新单个文档的示例 四.更新多个文档 4.1.更新多个文档的概述 4.2.更新多个文档的示例 五.使用upsert命令 5.1. ...

  6. MongoDB文档查询操作(三)

    关于MongoDB中的查询,我们已经连着介绍了两篇文章了,本文我们来介绍另外一个查询概念游标. 本文是MongoDB系列的第七篇文章,了解前面的文章有助于更好的理解本文: 1.Linux上安装Mong ...

  7. MongoDB文档查询操作(一)

    上篇文章我们主要介绍了MongoDB的修改操作,本文我们来看看查询操作. 本文是MongoDB系列的第五篇文章,了解前面的文章有助于更好的理解本文: 1.Linux上安装MongoDB 2.Mongo ...

  8. mongodb文档操作1

    mongodb文档操作1 mongodb文档操作1 插入操作 1. 使用方法insert()分别插入以下文档到集合stu中. 2. 使用方法insertMany()插入以下一组文档到集合stu中. 删 ...

  9. MongoDB 文档字段增删改

    MongoDB 基于CRUD(create,read,update,delete)方式实现了对集合上的文档进行增删改查.对于集合上字段的增删改,可以使用set或者unset修改器来实现.也可以使用文档 ...

最新文章

  1. SAP CRM one order框架到了S/4HANA后是如何重构的
  2. 通过ISO镜像文件安装Ubuntu(可实现默认启动Windows的双系统)
  3. JS partial-application
  4. netty大白话--字符串的收发(三)
  5. 3.11 随机初始化
  6. c语言课程设计类层次图,C语言课程设计————写下流程图! 谢谢
  7. 精心整理的10套最美Web前端新年特效---提前祝大家新年快乐(文末送书)
  8. H5调用手机拍照显示并压缩
  9. 启动优化之Android-Startup
  10. 5月1日起入境新加坡可使用IATA通行证,五一假期新加坡亲子游攻略
  11. matlab ptb安装,PTB之MATLAB编程:实验流程
  12. 50首经典好听的英文歌曲!
  13. 德律aoi程式制作_AOI入门介绍
  14. vb.net 获取系统图标_「快捷指令」桌面图标任意摆放
  15. 软件开发获取客户需求的十大沟通技巧
  16. windows无法连接到共享打印机,错误码:0x0000011b
  17. 什么是GPRS,CMWAP,CMNET-移动网络介绍
  18. 区块链上的自主身份之身份管理与身份应用
  19. 编程的首要原则是什么?
  20. c语言sql语句大全,在C语言中嵌入SQL语句.doc

热门文章

  1. Microsoft CryptoAPI加密技术(二)
  2. 苹果手机各种型号图片_2020年12月小米/红米手机推荐|小米/红米手机选购要点|小米/红米手机性价比推荐,干货满满...
  3. 获山东科技最高奖-农业大健康·万书波:沉醉谋定花生增产
  4. 恩施茶旅谋定乡村-农业大健康·万祥军:侗乡第一寨促生态
  5. 用SC命令 添加或删除windows服务提示OpenSCManager 失败5
  6. boost第 4 章 事件处理
  7. mysql数据库主从配置
  8. display:none和visible:hidden两者的区别
  9. python中字典的增删改查及其他常用操作
  10. Linux 基础——查看文件内容的命令