mongodbmongodb-oplog

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


When we insert a document, if the database and/or collection do not exist, they are create before the insert. Is this "create db/collection" operation available in the oplog?

The reason I'm interested, is because we want to upgrade from v2.4 to v3.2 but according to https://jira.mongodb.org/browse/SERVER-17634, the "applyOps" command will fail on insert ops if the collection is missing.


Solution

  • Yes, inserting to a new collection makes 2 entries in the oplog:

    1. {op: "c", o: {create: "name_of_the_collection"}} to create the collection

    2. {op: "i", ns: "name_of_the_collection", o:{the document}} to insert the document

    Answering the follow up questions:

    The official documentation reads:

    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" include create collection, database etc. If collection does not exists on primary member, it's created on first insert: https://github.com/mongodb/mongo/blob/v3.2/src/mongo/db/catalog/database.cpp#L454

    When new collection created, the corresponding document recorded to the oplog: https://github.com/mongodb/mongo/blob/v3.2/src/mongo/db/catalog/database.cpp#L511

    Same logic apply to other commands, including create database.

    Format of oplog documents contains all necessary information to replay all operations on correct database, collection, and document:

    /* 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'
    */