0

I'm modifying document inserted in one MongoDB relpSet and inserting it in another using mongo-oplog and node.js

I'm following this link for inserting documents in mongo using node.js. I wrote a wrapper which is getting the document to be inserted as argument.

function insertMongo(doc) {
    var insertDocument = function (db, callback) {
        db.collection('new').insertOne(doc, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted a document into the collection,", result);
            callback();
        });
    };


    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);
        insertDocument(db, function () {
            db.close();
        });
    });
}

I'm calling this wrapper function as a callback for the function (parseJSON) that creates the json document to be inserted.

function parseJSON(data, callback)
{
    var parsedData = {};
    // logic to modify JSON taken from oplog
    // returns populated parsedData object
    callback(parsedData);
}

I'm calling parseJSON inside mongo-oplog like:

oplog.on('insert', function (doc) {
  var a = JSON.parse(JSON.stringify(doc.o));
  parseJSON(a, insertMongo);
});

This is getting into an infinite loop with output like this, after inserting the document:

o: { _id: 56d99e4a95ed520a0814d87a } }
doc { _id: '56d99e4a95ed520a0814d87a' }
{ ts: { _bsontype: 'Timestamp', low_: 41, high_: 1457102410 },
    h: { _bsontype: 'Long', low_: -249545631, high_: 547021855 },
    v: 2,
        op: 'i',
    ns: 'user.new',
    o: { _id: 56d99e4a95ed520a0814d87b } }
doc { _id: '56d99e4a95ed520a0814d87b' }
Inserted a document into the collection,
{ ts: { _bsontype: 'Timestamp', low_: 42, high_: 1457102410 },
    h: { _bsontype: 'Long', low_: -1687338339, high_: 1742865608 },
    v: 2,
    op: 'i',
    ns: 'user.new',
    o: { _id: 56d99e4a95ed520a0814d87c } }
doc { _id: '56d99e4a95ed520a0814d87c' }
Inserted a document into the collection,

Any help is appreciated. Thank you.

Shipra
  • 1,259
  • 2
  • 14
  • 26

1 Answers1

0

it looks like you are caling insert from onInsert event

oplog.on('insert', function (doc) {

var a = JSON.parse(JSON.stringify(doc.o));

parseJSON(a, insertMongo);

});

so try to remove this line for a test:

 parseJSON(a, insertMongo);
Community
  • 1
  • 1
profesor79
  • 9,213
  • 3
  • 31
  • 52
  • I'm modifying document inserted in one MongoDB and inserting it in another, hence I have to call insert from onInsert event. I've edited the question for more clarity. Thanks. – Shipra Mar 05 '16 at 04:52