2

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.

  • Check out this link https://stackoverflow.com/questions/32019267/how-to-properly-do-a-bulk-upsert-update-in-mongodb Maybe you should separate updateOne and upsert – Rashad Ibrahimov Mar 13 '18 at 20:37
  • Have you got this resolved? I am facing exactly the same issue, was it a driver compatibility issue?. I am on Mongo v4.0.3. Node.js v12.17.0, and Node.js driver 3.1.2. Nothing seem wrong with the syntax from my read of mongo doc. – BJYC Jun 22 '20 at 23:39

0 Answers0