'Read updated documents after bulk write

I have a requirement to perform a bulk update of documents and return the updated set of documents to the user.

Currently, I'm updating the documents using the updateOne operation in the bulk write api:

  const result = await db.collection(collectionName)
    .bulkWrite(updates.map(({ _id, update }) =>
      ({
        updateOne: {
          filter: { _id: new ObjectId(_id) },
          update: update
        }
      })
    ))

Unlike, findOneAndModify I'm unable to retrieve the updated documents with this method. So, as an alternative, I am planning to use find immediately after bulkWrite to retrieve the updates. I'm concerned with how this would work at scale with a large set of replicas and shards. Due to eventual consistency, I may be retrieving a version of the document prior to applying the update.

This is not desired behaviour, so I am considering specifying a write concern to the bulkWrite. Which write concern would guarantee a consistent read immediately after bulkWrite? And what would be the performance implications?

Alternatively, is there a better approach to implementing this? Perhaps by making multiple findAndModify calls? I need to support updating about 100 documents in total.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source