Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Query MongoDB oplog.rs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Oplog is Capped Collection and allocates 5% of the free disk space by default.

Let's take a look at oplog.rs:

Rs_test:SECONDARY > use localswitched to db localrs_test:SECONDARY > db.oplog.rs.findOne () {"ts": Timestamp (1465879171), "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 ts:8 bytes, represented by 4-byte unix timestamp + 4-byte self-increment count.

This value is important, and when electing a new primary (such as when a master goes down), the secondary with the largest ts is selected as the new primary.

The operation type of the op:1 byte, for example, I for insert,d for delete.

Ns: the namespace in which the operation is located.

O: the document corresponding to the operation, that is, the content of the current operation (such as the fields and values to be updated during the update operation)

O2: the condition when performing an update operation, which is available only when update.

Op can be one of the following situations:

"I": insert

"u": update

"d": delete

"c": db cmd

"db": declares the current database (where ns is set to = > database name +'.')

"n": no op, or null operation, which is performed periodically to ensure timeliness. Modifying the configuration will result in an "n" operation.

Query example

The oplog.rs system collection is only used for replication, cannot create an index, and the query statement will be 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 the oplog within one hour

> var SECS_PER_HOUR = 3600 > var now = Math.floor ((new Date (). GetTime ()) / 1000) / / seconds since epoch right now > db.oplog.rs.find ({"ts": {"$lt": Timestamp (now, 1), "$gt": Timestamp (now-SECS_PER_HOUR, 1)}}

Query the oplog within a certain period of time (of course, note that the time is UTC storage)

> var since = Math.floor (ISODate ("2014-08-12T09:00:00.000Z"). GetTime () / 1000) > var until = Math.floor (ISODate ("2014-08-12T15:00:00.000Z"). GetTime () / 1000) > db.oplog.rs.find ({"ts": {"$lt": Timestamp (until, 1), "$gt": Timestamp (since, 1)}))

Aggregate statistics on the amount of UPDATE operations of each set

> db.oplog.rs.aggregate ([{$match: {"op": "u"}}, {$group: {_ id: "$ns", count: {$sum:1}])

Oplog referenc

Replica Set Oplog

Https://docs.mongodb.com/manual/core/replica-set-oplog/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report