node.jsmongodbmongodb-oplog

Hi, I wanted to know to know if there is way to print out mongo oplog into a textfile


I have downloaded MongoDB and setup replication on my computer. I also have node.js running and it is connected to mongodb. I am trying to print it out in textfile or even to console. My database is small right now, and I just wanted to test the connection


Solution

  • here is a github project: https://github.com/cayasso/mongo-oplog

    The sample code below will show the log to the console.

    var MongoOplog = require('mongo-oplog');
    var oplog = MongoOplog('mongodb://127.0.0.1:27017/local', { ns: 'test.posts' }).tail();
    
    oplog.on('op', function (data) {
      console.log(data);
    });
    
    oplog.on('insert', function (doc) {
      console.log(doc.op);
    });
    
    oplog.on('update', function (doc) {
      console.log(doc.op);
    });
    
    oplog.on('delete', function (doc) {
      console.log(doc.op._id);
    });
    
    oplog.on('error', function (error) {
      console.log(error);
    });
    
    oplog.on('end', function () {
      console.log('Stream ended');
    });
    
    oplog.stop(function () {
      console.log('server stopped');
    });