Oplog is the capped Collection, which allocates 5% of the free disk space by default.
Look at the look of oplog.rs:
rs_test:secondary> use localswitched to db localrs_test:secondary> Db.oplog.rs.findOne (); { "TS" : timestamp (1465879171, 238), "H" : numberlong (" -2275413922284641862"), "V" : 2, "Op" : "U", "ns" : "mydb.synctable", "O2" : { "_id" : "bbf80260-3d58-49f1-9c8c-e093d5d57527" }, "O" : { "_id" : " bbf80260-3d58-49f1-9c8c-e093d5d57527 ", "EntityId" : "362569", "TypeName" : "Product", "Times" : 14208, "Createtime" : isodate (" 2014-11-15t14:35:51.916z "), "LastModified" : isodate ("2016-06-14t04:38:21.708z"), "Lastoperationtime" : Isodate ("2016-06-14t04:39:30.957z") }}
Field meaning
The timestamp of the ts:8 byte, represented by a 4-byte UNIX timestamp + 4-byte self-increment count.
This value is important, and when the new primary is elected (such as Master down), the secondary of the largest TS is chosen as the new primary.
The operation type of the op:1 byte, for example I means that insert,d represents delete.
NS: The namespace where the operation is located.
O: The document that corresponds to the operation, that is, the contents of the current operation (such as the fields and values to be updated when the update operation)
O2: The condition at the time of the update operation is limited to update only.
Where op can be one of the following scenarios:
"I": Insert
"U": Update
"D": Delete
"C": db cmd
"DB": Declares the current database (where NS is set to = + database name + '. ')
"N": No op, which is an empty operation, which is periodically executed to ensure timeliness. Modifying the configuration will result in an "n" operation.
query Example
The Oplog.rs system collection is for replication only, cannot create indexes, and query statements are slow.
> db.oplog.rs.find ({},{"ts": 1}). Sort ({$natural:-1}) {"TS": Timestamp (1406185666, 1)} {"TS": Timestamp (1406180043, 1)} {"TS": Timestamp (1406180033, 1)} {"TS": Timestamp (1406172831, 1)} {"TS": Timestamp (1406171938, 1)} > Db.oplog.rs.find ({"TS": {"$gte": Timestamp (1406185630, 1)}},{"ts": 1})
Query for Oplog within one hour
> var secs_per_hour = 3600> var now = Math.floor ((New Date (). GetTime ())/+)//seconds since epoch right now> Db.oplog.rs.find ({"TS": {"$lt": Timestamp (now, 1), "$GT": Timestamp (Now-secs_per_hour, 1)}}
Query for Oplog within a certain time period (of course, note that UTC is stored)
> var since = Math.floor (Isodate ("2014-08-12t09:00:00.000z"). GetTime ()/+) > var until = Math.floor (Isodate (" 2014-08-12t15:00:00.000z "). GetTime ()/> Db.oplog.rs.find ({" TS ": {" $lt ": Timestamp (until, 1)," $GT ": Timestam P (since, 1)}})
Aggregate statistics update operations for each collection
> db.oplog.rs.aggregate ([{$match: {"op": "U"}}, {$group: {_id: "$ns", count:{$sum: 1}}])
Oplog Reference
Replica Set Oplog
https://docs.mongodb.com/manual/core/replica-set-oplog/
This article is from the SQL Server deep dives blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1789178
Query MongoDB oplog.rs