Question

I have this Node.js application that it gets a large sum of JSON data with unique ids and then inserts them into a MongoDB using Mongojs.

The problem is that in this large array of data, there might be some duplicate documents! so when using Mongojs to insert these data, it returns an error E11000 duplicate key error and wouldn't continue the insertion.

So what I'm looking for is something like the continueOnError/keepGoing option of native Mongo. It would ignore duplicates and continue the insert.
http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#insert

This is my code if it helps:

var mongojs = require('mongojs'),
dburl = process.env.MONGOHQ_URL || 'mongodb://localhost/test',
db = mongojs(dburl, ['concerts']);

function insertData(data) {
db.concerts.insert(data, {keepGoing: true, safe: true}, function(err, docs){
    console.log('err: ' + err + '  docs:' + docs);
    if(!err) console.log('data inserted successfully!\n');
});
}

exports.insertData = insertData;

Here in this code I have set option to true, but it still does not work (the same error).
Is there anyway that I can do this with Mongojs or do I have to use native-mongo?

Thanks in advance.

Was it helpful?

Solution

This solved my problem, just in case.

db.concerts.insert(data, {continueOnError: true, safe: true}, function(err, docs){
       console.log('err: ' + err);
       if(!err) console.log('data inserted successfully!\n');
   });

Source: https://github.com/gett/mongojs/issues/58

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top