What's the correct syntax for doing a NodeJS/MongoDB updateOne with arrayFilters in a bulk operation?
MongoDB version: 4.2.5
NodeJS version: 12.x
npm mongodb version: 3.6.0
Given the following simplified collection. I need to update an element in the lineItems array.
{
_id: ObjectId("5d50689fd304e3189aae99ba"),
"lineItems" : [
{ "importId" : "abc123" },
{ "importId" : "def456" }
]
}
The following bulk update works in the MongoDB shell:
var bulk = db.myCollection.initializeOrderedBulkOp();
bulk
.find({"_id": ObjectId(myId)})
.arrayFilters( [ { "elem.importId": "abc123" } ] )
.updateOne( { $set: { "lineItems.$[elem].meta": { "test": 1 } } } );
bulk.execute();
The following works in NodeJS (but it's NOT a bulk operation):
db.collection('myCollection').updateOne(
{ _id: ObjectID(myId) },
{ $set: { 'lineItems.$[elem].meta': { test: 1 } } },
{ arrayFilters: [{ 'elem.importId': lineItem.importId }] }
);
This ticket implies that the NodeJS MongoDB driver supports arrayFilters on bulk operations:
https://jira.mongodb.org/browse/NODE-1911
However, I've tried many different variations. But for example, this code returns error "arrayFilters is not a function".
const bulkOp = db.collection('myCollection').initializeOrderedBulkOp()
bulkOp
.find({ _id: ObjectId(myId) })
.arrayFilters([{ 'elem.importId': lineItem.importId }])
.updateOne({ $set: { 'lineItems.$[elem].meta': { test: 1 } } });
The above is based on the example in the MongoDB documentation:
bulk
.find({}).
arrayFilters( [ { "elem.grade": { $gt: 85 } } ] ).
updateOne( { $set: { "grades.$[elem].mean" : 70 } } );
https://docs.mongodb.com/v4.2/reference/method/Bulk.find.arrayFilters/#Bulk.find.arrayFilters
所有评论(0)