DauleDK
DauleDK

Reputation: 3443

Is it possible to tell if any changes was made to a mongodb database, using the oplog?

My object is to improve the performance of our integration tests. This made me think if it would be possible to detect if any changes was made to MongoDB?

We run our tests against the in-memory MongoDB server, and in the beforeEach hook I would therefore like to prune+seed the database, but only if any changes occurred. Our in-memory database uses replica set, and we use transactions.

What I wan't

A way to determine if there had been any changes (insert, update, delete) to our database.

What I have tried

I tried using a count on the oplog, using the aggregation framework:

{
  $match: {
    ns: /<db-name>/,
    op: { $in: ['u', 'i', 'd'] },
  },
},
{ $count: 'count' },

This does not seem to work.

I also tried to just query the latest oplog document, and save the ui binary field, and compare later. However, it seems like this ID can stay the same, despite what I define as changes happening.

Upvotes: 0

Views: 267

Answers (1)

R2D2
R2D2

Reputation: 10737

You can tail oplog to check changes , but the better option introduced in mongodb 3.6 and above is to use change streams:

https://docs.mongodb.com/manual/changeStreams

example watching changes in real time made in exampleDB database from console mongo client:

 watchCursor = db.getSiblingDB("exampledb").watch()

 while (!watchCursor.isExhausted()){
   if (watchCursor.hasNext()){
       printjson(watchCursor.next());
    }
 }

You can also watch changes at collection level as follow:

  watchCursor = db.getSiblingDB("exampleDB").exampleCOL.watch()

  while (!watchCursor.isExhausted()){
      if (watchCursor.hasNext()){
      printjson(watchCursor.next());
  }
 }

Upvotes: 1

Related Questions