Reading mongoDB Binlogs

6.1k views Asked by At

In mysql I debug how a record has changed by doing

mysqlbinlog bin-88.log | grep "record-id" --before=2 --after=2

How do I do something similar with mongo?

Thanks!

2

There are 2 answers

0
Eve Freeman On

There is an oplog in mongodb, but it only gets populated when you have a replica set. It's a capped collection.

You can query it from the shell like so:

use local;
db.oplog.rs.find(); // specify "ns" and "_id" here, if desired
2
Stennie On

You can use a similar approach in MongoDB. The equivalent of the binlog is the oplog which (like the MySQL binlog) is normally used for replication. In MongoDB the oplog is a capped collection called oplog.rs which lives in the local database. Being a capped collection, the oplog keeps a fixed amount of history. You can change the size of the oplog.

In order for MongoDB to create an oplog, you do need to configure your mongod to use replication. If you only want the oplog, you can create a dummy replica set with only one node.

Note: the recommended production configuration is to use replication with a minimum of three nodes as this provides for failover and data redundancy.

A helpful series of blog posts explaining the oplog format starts with: Replication Internals. You can query this as you would a normal MongoDB collection, so you can try some updates and see what the resulting commands are.

Example of finding all updates for a given object ID (o2._id) in a given name space (mydb database, mycollection):

> use local
> db.oplog.rs.find({'ns':'mydb.mycollection', 'o2._id' : ObjectId("501c87fa9d2b5b2b54437125")})
{
    "ts" : Timestamp(1344047454000, 1),
    "h" : NumberLong("226994175309144171"),
    "op" : "u",
    "ns" : "mydb.mycollection",
    "o2" : {
        "_id" : ObjectId("501c87fa9d2b5b2b54437125")
    },
    "o" : {
        "name" : "Bobby Tables",
        "iq" : 180
    }
}

In the above example I found an update entry (op: u) for the document with ObjectID 501c87fa9d2b5b2b54437125. There were two column updates applied: name, iq.

One difference to note from MySQL binlogs is that the MongoDB oplog is a list of idempotent changes to apply; the MySQL binlog is a list of statements that modify data. The oplog format allows MongoDB to safely apply entries more than once without unwanted side effects. It also means that you will find the results of an update command affecting multiple documents as separate entries for each document in the oplog rather than a single "UPDATE .. " statement as would appear in a binlog.