Location>code7788 >text

Mongo oplog understanding

Popularity:562 ℃/2024-10-19 10:06:02

Reprinted with attribution:

The oplog (operation log) is the log used in MongoDB to record all write operations. It is a special collection that is stored in the master node of the replica set. oplog is used to ensure that the sub-nodes in the replica set keep the same data as the master node. When the master node performs a write operation, the corresponding operation is logged into the oplog, and the sub-node reads the oplog to get the latest data changes.

data structure

The oplog is under local data in the mongo database master node. You can access the local database to view the structure of the data stored in this collection:

Go to the oplog database:

use local

View the data structure:

rs0:PRIMARY> ()
{
        "ts" : Timestamp(1729001735, 165),
        "t" : NumberLong(187),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "",
        "ui" : UUID("94a529f7-dbf1-4be0-b296-361094515e13"),
        "wall" : ISODate("2024-10-15T14:15:35.636Z"),
        "lsid" : {
                "id" : UUID("bbf4c6e6-e139-48e2-82df-a0b15bd0a89f"),
                "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
        },
        "txnNumber" : NumberLong(29170),
        "stmtId" : 0,
        "prevOpTime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
        },
        "o" : {
                "_id" : "75a4d50f-8f37-4d06-8abe-d1c2b0de97a0",
                "reason" : "link[maipu_3:gigabitethernet3>maipu_2:gigabitethernet3] has no label",
                "type" : "LINK",
                "key" : "maipu_3:gigabitethernet3>maipu_2:gigabitethernet3",
                "tePolicyColorTupleId" : "maipu_3_maipu_1_MaiPu31_3628",
                "_class" : ""
        }
}
rs0:PRIMARY>

The oplog is an infinitely growing collection containing the following main fields:

  • ts: timestamp indicating the time of the operation.
  • h: hash value that uniquely identifies the operation.
  • op: operation type, possible values include "i" (insert), "u" (update), "d" (delete).
  • ns: namespace indicating the database and collection to which the operation belongs.
  • o: details of the operation, depending on the type of operation.

characterization