Home > OS >  Bulk.getOperations() in MongoDB Node driver
Bulk.getOperations() in MongoDB Node driver

Time:10-11

I'd like to view the results of a bulk operation, specifically to know the IDs of the documents that were updated. I understand that this information is made available through the Bulk.getOperations() method. However, it doesn't appear that this method is available through the MongoDB NodeJS library (at least, the one I'm using).

Could you please let me know if there's something I'm doing wrong here:

const bulk = db.collection('companies').initializeOrderedBulkOp()
const results = getLatestFinanicialResults() // from remote API

results.forEach(result => 
  bulk.find({ 
    name: result.companyName, 
    report: { $ne: result.report } 
  }).updateOne([
    { $unset: 'prevReport' },
    { $set: { prevReport: '$report' } },
    { $unset: 'report' },
    { $set: { report: result.report } }
  ]))

await bulk.execute()
await bulk.getOperations() // <-- fails, undefined in Typescript library

I get a static IDE error:

Uncaught TypeError: bulk.getOperations is not a function

CodePudding user response:

I'd like to view the results of a bulk operation, specifically to know the IDs of the documents that were updated

As of currently (MongoDB server v6.x) There is no methods to return IDs for updated documents from a bulk operations (only insert and upsert operations). However, there may be a work around depending on your use case.

The manual that you linked for Bulk.getOperations() is for mongosh, which is a MongoDB Shell application. If you look into the source code for getOperations() in mongosh, it's just a convenient wrapper for batches'. The method batches` returns a list of operations sent for the bulk execution.

As you are utilising ordered bulk operations, MongoDB executes the operations serially. If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.

Depending on the use case, if you modify the bulk.find() part to contain a search for _id for example:

bulk.find({"_id": result._id}).updateOne({$set:{prevReport:"$report"}});

You should be able to see the _id value of the operation in the batches, i.e.

await batch.execute(); 
console.log(JSON.stringify(batch.batches));

Example output:

{
 "originalZeroIndex":0,
 "currentIndex":0,
 "originalIndexes":[0],
 "batchType":2,
 "operations":[{"q":{"_id":"634354787d080d3a1e3da51f"},
                "u":{"$set":{"prevReport":"$report"}}], 
 "size":0,
 "sizeBytes":0
}

For additional information, you could also retrieve the BulkWriteResult. For example, the getLastOp to retrieve the last operation (in case of a failure)

  • Related