mongodb oplog:"insert" op 添加 "create collection" op 吗?

mongodb oplog: does "insert" op add "create collection" op?

当我们插入一个文档时,如果数据库and/or集合不存在,它们是在插入之前创建的。这个 "create db/collection" 操作在 oplog 中可用吗?

我感兴趣的原因是因为我们想从 v2.4 升级到 v3.2,但是根据 https://jira.mongodb.org/browse/SERVER-17634,如果集合,"applyOps" 命令将在插入操作时失败不见了。

是的,插入新集合会在操作日志中生成 2 个条目:

  1. {op: "c", o: {create: "name_of_the_collection"}} 创建集合

  2. {op: "i", ns: "name_of_the_collection", o:{the document}}插入文档

回答后续问题:

official documentation 内容为:

The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases.

"all operations" 包括创建集合、数据库等。如果主要成员上不存在集合,则在第一次插入时创建:https://github.com/mongodb/mongo/blob/v3.2/src/mongo/db/catalog/database.cpp#L454

创建新集合时,对应的文档记录到oplog:https://github.com/mongodb/mongo/blob/v3.2/src/mongo/db/catalog/database.cpp#L511

相同的逻辑适用于其他命令,包括创建数据库。

Format of oplog documents 包含在正确的数据库、集合和文档上重播所有操作的所有必要信息:

/* we write to local.oplog.rs:
     { ts : ..., h: ..., v: ..., op: ..., etc }
   ts: an OpTime timestamp
   h: hash
   v: version
   op:
    "i" insert
    "u" update
    "d" delete
    "c" db cmd
    "db" declares presence of a database (ns is set to the db name + '.')
    "n" no op
   bb param:
     if not null, specifies a boolean to pass along to the other side as b: param.
     used for "justOne" or "upsert" flags on 'd', 'u'
*/