How to move tail-cursor using awaitdata to the end so I just get new updates

I am trying to look at the MongoDB oplog with the node.js driver, and it works in theory, but it has been taking quite a while because it seems to be scanning the entire collection. I found this in MongoDB docs:

  • Since tail cursors do not use indexes, initial query scans can be expensive; but after initially retrieving the cursor, subsequent retrieving of newly added documents is inexpensive.

Is there a way to quickly “exhale” the cursor just to start the tail? It seems to me that the guys from Meteor have solved this, but I have problems understanding the difference from reading their code. This is what I have now:

var cursorOptions = {
    tailable: true,
    awaitdata: true,
    numberOfRetries: -1
};

var oplogStream = oplogDb.collection('oplog.rs').find(
    {
        ns: { $regex : /^dbname\./ },
        op: "i",
        ts: { $gt: lastEntry.ts } 
    },
    cursorOptions
).sort({$natural: -1}).stream();

oplogStream.on('data', publishDocument);

oplogStream.on('end', function() {
    log.error("received unexpected end event from oplog watcher.");
});
+3
1

, 5 . :

oplogReplay true. , ts. , , . , , :

var cursorOptions = {
  tailable: true,
  awaitdata: true,
  oplogReplay: true, // add this line
  numberOfRetries: -1
};
+3

Source: https://habr.com/ru/post/1656862/


All Articles