0

我正在修改插入一个 MongoDB relpSet 的文档,并使用 mongo-oplog 和 node.js 将其插入另一个

我正在关注此链接以使用 node.js 在 mongo 中插入文档。我写了一个包装器,它让文档作为参数插入。

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();
        });
    });
}

我将此包装函数调用为创建要插入的 json 文档的函数 (parseJSON) 的回调。

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

我在 mongo-oplog 中调用 parseJSON,例如:

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

插入文档后,这将进入无限循环,输出如下:

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,

任何帮助表示赞赏。谢谢你。

4

1 回答 1

0

看起来您正在从 onInsert 事件中调用插入

oplog.on('插入',函数(文档){

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

parseJSON(a, insertMongo );

});

所以尝试删除这一行进行测试:

 parseJSON(a, insertMongo);
于 2016-03-04T15:25:13.310 回答