0

I want to query the oplog to find what are the operation made in a particular time. How it possible to find and query oplog in MongoDB. Where oplog is placed? Please explain with an example...I could'nt find any tutorial over internet...

  • It's just a collection and also only present with replica sets. The manual is your best place to read: http://docs.mongodb.org/manual/core/replica-set-oplog/ – Neil Lunn Sep 06 '14 at 03:59

2 Answers2

2

If you want to query by date, you need to generate a proper MongoDB timestamp. You can use the Date class for that purpose.

If you want for example to count the amount of entries between two dates, you can try a query similar to this one:

db.oplog.rs.count(
    {
        ts: 
        {
            $gt: Timestamp(new Date("2020-02-01").getTime() / 1000, 1), 
            $lt: Timestamp(new Date("2020-03-01").getTime() / 1000, 1)
        }
    }
);

You have to always divide the Date milliseconds by 1000, because the Timestamp class operates with seconds after 1970-01-01.

Here is another example query which excludes some very usual entries in the oplog, like e.g "periodic noop":

db.oplog.rs.find(
    {
        ts: 
        {
            $gt: Timestamp(new Date("2020-02-01").getTime() / 1000, 1), 
            $lt: Timestamp(new Date("2020-03-01").getTime() / 1000, 1)
        },
        "o.msg":
        {
            $ne: "periodic noop"
        },
        "ns" : {
            $ne: "config.system.sessions"
        }
    }
).limit(5).pretty();
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
0

Simple query to read from oplog

 query = {'ts': {'$gt': some_timestamp}}  # Replace with your own query.
        cursor = db.oplog.rs.find(query, **_TAIL_OPTS)
HaBo
  • 13,999
  • 36
  • 114
  • 206