MongoDB:批量操作是否作为一个整体写入 oplog? [英] MongoDB: Are bulk operations written to the oplog as a whole?

查看:20
本文介绍了MongoDB:批量操作是否作为一个整体写入 oplog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 MongoDB 3 中发出一个有序的批量操作时,批量操作是否作为一个整体写入 oplog,以便在服务器崩溃后可以作为一个整体重放?

When I issue an ordered bulk operation in MongoDB 3, is the bulk operation as a whole written to the oplog so that it can be replayed as a whole after a server crash?

这个问题的基本原理如下:

The rationale for this question is the following:

我知道没有真正的事务,但我可以使用 $isolated 关键字来获得一些读取一致性(在某些情况下).除了良好的模式设计与否之外,让我们假设我必须一次更新可能不同集合中的多个文档,这将是 SQL 中的事务.我不在乎数据在任何时候都处于不一致状态,但是我确实要求数据最终保持一致.因此,虽然我可能不关心操作过程中的错误和丢失的回滚,但我需要在某些时候完全执行根本不执行更新序列,在以使它们能够在批量操作期间(例如,由于随机 CoreOS 更新)在意外的服务器故障或关闭时幸免于难.

I know that there are no real transactions but that I can use the $isolated keyword to have some read consistency (in some cases). Apart from being good schema design or not, let's assume that I would have to update multiple documents in possibly different collections in one go, in what would be a transaction in SQL. I do not care about the data being in an inconsistent state at any near moment, however I do require the data to be consistent eventually. So, while I may not care about errors and missing rollbacks during the operation, I require the sequence of updates to be performed entirely or not performed at all at some point, in order to have them survive unexpected server failures or shutdowns in the middle of the bulk operation (because of, say, random CoreOS updates).

推荐答案

我会以一般性警告进入这一点,我承认我什至没有看过结果,但基本原则从一开始就对我来说是有效的.

I will enter into this with the general caveat that I admit I have not even looked at the results, but the basic principles seem valid to me from the start.

这里您需要考虑的是在一般调用中呈现的漂亮的语法糖"的幕后实际发生了什么".这意味着基本上查看您正在调用的操作的命令形式"实际上做了什么.在这种情况下 "update".

What you need to consider here is "what is actually happening under the hood" of the "nice syntax sugar" you are presented with in general calls. What this means is basically looking at what the "command form" of the operations you are calling actually do. In this case "update".

因此,如果您已经看过该链接,请考虑以下 "批量"更新表单:

So, if you had a look at that link already, then consider the following "Bulk" update form:

var bulk = db.collection.initializeOrdedBulkOp();

bulk.find({ "_id": 1 }).updateOne({ "$set": { "a": 1 } });
bulk.find({ "_id": 2 }).updateOne({ "$set": { "b": 2 } });

bulk.execute();

现在您已经知道这是作为一个请求发送到服务器的,但您可能没有考虑的是,幕后"发出的实际请求"实际上是这样的:>

Now you already know this is being sent to the server as one request, but what you are likely not considering is that actual "request" made "under the hood" is actually this:

db.runCommand({
    "update": "collection",
    "updates": [
        { "q": { "_id": 1 }, "u": { "$set": { "a": 1 } } },
        { "q": { "_id": 2 }, "u": { "$set": { "b": 2 } } }
    ],
    "ordered": true
})

因此,您在更新"操作下的日志中实际看到的内容实际上类似于(从完整输出缩写为仅查询):

Therefore it stands to reason that what you actually see in the logs under the "update" operation is actually something like (abbreviated from full output to just the query):

{ "q": { "_id": 1 }, "u": { "$set": { "a": 1 } } }
{ "q": { "_id": 2 }, "u": { "$set": { "b": 2 } } }

因此,这意味着具有关联命令的每个操作都在 oplog 中,用于在复制和/或您可能执行的其他操作(例如专门重放"操作日志条目)时重播".

Which therefore means that each of those actions with the associated command is in the oplog for "replay" on replication and/or on other actions you might perform such as specifically "replaying" the oplog entries.

我确信这就是实际发生的事情,甚至无需查看,因为我知道驱动程序如何实现实际调用,并且每个调用都以这种方式保存在 oplog 中是有道理的.

I'd be sure that is what actually happens without even looking, because I know that it how the drivers implement the actual calls, and it makes sense that each call is kept within the oplog in this way.

因此作为一个整体",则不.这些不是事务",并且始终是不同的操作,即使它们的提交和返回在单个请求中也是如此.但它们不是单一的操作,因此不会也不应该被这样记录.

Therefore "as a whole", then no. These are not "transactions" and are always distinct operations even if their submission and return are within a singular request. But they are not a singular operation, and therefore will not and should not be recorded as such.

这篇关于MongoDB:批量操作是否作为一个整体写入 oplog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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