在MongoDB中批量插入原子吗 [英] Are bulk inserts atomic in MongoDB

查看:100
本文介绍了在MongoDB中批量插入原子吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习mongodb.如果我创建批量写入,则此事务是全部还是全部?我有一种情况,我的用户可以删除他们的朋友.

I am learning about mongodb. If I create a bulk write is this transaction all or nothing? I have a scenario where my users can delete who they are friends with.

FRIEND 1  |  FRIEND 2
  User B     USER A
  User A     USER B

为此,我需要从两个双向关系中删除.为了保持一致性,我需要使它们全部或全部不出现,因为我不希望2个操作中只有1个成功,因为这会导致不良数据.阅读文档,我找不到答案:

For this to happen I need to delete from both bidirectional relationships. For consistency I need these to occur as a all or nothing because I wouldn't want only 1 of the 2 operations to succeed as this would cause bad data. Reading the docs I could not find the answer:

https://docs.mongodb.org/manual/core/bulk -write-operations/

推荐答案

db.collection.initializeOrderedBulkOp() 如果在处理其中一个写入操作期间发生错误,MongoDB将返回而不处理列表中的任何剩余的写入操作."

db.collection.initializeOrderedBulkOp() "If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list."

没有提及回滚操作,只是停止插入其余操作.

No mention of rollback ops, simply stops inserting the remaining.

db.collection.insert()方法 "insert()方法在传递文档数组时执行批量插入,并自动插入每个文档."

db.collection.insert() method "The insert() method, when passed an array of documents, performs a bulk insert, and inserts each document atomically."

您可以自己滚动.但请使用已确认的写入问题,该问题必须通过您选择的驱动程序来实现. shell被确认,但驱动程序可能未被确认.

you can roll your own . but use acknowledged write concern which would have to be via your chosen driver. shell is acknowledged but perhaps driver is not.

https://docs.mongodb.org/manual/core/write-concern /

try
   insert 1
catch 
  delete

try
   insert 2
catch 
  delete 1
  delete 2

这篇关于在MongoDB中批量插入原子吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆