I'm currently working on a project where I am trying to do some bulk writes using MongoDB's native driver on Node.js. I need to periodically update/upsert a couple hundred documents but the operation isn't updating/upserting all documents. Even in an empty collection, I'm only getting ~130-180 documents inserted out of the total 230 or so. Below is my code:
const documents = [ /* Array of ~210 objects */ ]
const bulkUpdate = db.collection('notes').initializeUnorderedBulkOp()
documents.forEach(document => {
bulkUpdate
.find({ name: document.name })
.upsert()
.updateOne({
$set: {
message: document.message,
lastModified: new Date()
},
$setOnInsert: {
name: document.name,
dateCreated: new Date()
}
})
}
bulkUpdate.execute()
.then(results => {
console.log(results.nUpserted, results.nMatched, results.nModified)
}
.catch(error => {
console.error('Error updating 'notes' collection.)
}
When I run this when the collection doesn't exist, the result I'm getting is: 168 45 45
. How is 45 documents matched when the collection didn't even exist in the beginning, and modified (yet, they are not in the collection)? And only 168 documents of the 213 are inserted.
At this point, I've tried everything and searched everywhere. I've tried using both initializeUnorderedBulkOp()
and initializeOrderedBulkOp()
, I've also tried removing the unique index I have on the name
field. I also tried with bulkUpdate.execute({ j: 1 })
write concern and still nothing.
Would really appreciate it if someone could shed some light on this matter.
Thanks!
Edit: Forgot to mention that I am using a sandboxed mLab hosted MongoDB.