I am using the native Node.js MongoDB driver to insert multiple documents at once. I have an array of javascript objects (where each object represents a user), and I am using the following code.
var col = db.collection('users');
col.insertMany(usersArray, function(err, r) {
if (err) {
log('Bulk Insert Error', err);
} else {
log('Successful Insert', r.insertedCount);
db.close();
}
});
Each user document in the usersArray
array has an email field. I do not want to insert objects where a document with the email already exists in the users
collection, as I have modelled emails to be unique. Such documents should be skipped in the query.
I know this would be achievable if usersArray
had an _id
field (where I would use continueOnError: true
), but unfortunately my usersArray
is from a separate source. Doing single inserts is inefficient, as my usersArray
can have well over ~1000 objects.
The work around I have thought of is selecting all documents from db and reconstructing the usersArray
with only unique documents in my node application, to reduce the number of requests to MongoDB. Is there a way the collection.insertMany()
function can be modified to do this? Raw MongoDB queries will also be appreciated.