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

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

问题描述

当我在MongoDB 3中发出有序的批量操作时,批量操作是否整体写入了操作日志中,以便在服务器崩溃后可以整体重播?

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.

您需要在这里考虑的是在常规调用中出现的漂亮语法糖"的幕后实际情况".基本上,这意味着查看您正在调用的操作的命令形式"实际上是做什么的.在这种情况下,更新" .

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 } } }

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

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.

我可以肯定这是什至没有看就发生的事情,因为我知道驱动程序如何实现实际的调用,并且每个调用都以这种方式保存在操作日志中是很有意义的.

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:批量操作是否整体写入了操作日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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