I have the following updateMany
operation in MongoDB:
myCollection.updateMany({
_id: {
$in: myIds,
}
}, [
{
$set: {
"nested.field": {
$arrayToObject: {
$filter: {
input: {
$objectToArray: "nested.field",
},
cond: {
$gte: [
"$$this.k",
new Date().toISOString()
],
}
}
}
}
}
}
])
Basically what it does is to filter out all the elements inside $.nested.field
with a key less than the current date.
I am trying to put this inside a bulk op (together with other operations), but I get an error (it works correctly when used with a simple updateMany
).
const bulkOp = myCollection.initializeUnorderedBulkOp();
bulkOp.find({
_id: {
$in: myIds
},
}).update([
{
$set: {
"nested.field": {
$arrayToObject: {
$filter: {
input: {
$objectToArray: "nested.field",
},
cond: {
$gte: [
"$$this.k",
new Date().toISOString()
],
}
}
}
}
}
}
]);
await bulkOp.execute();
Executing the above I get the following error: MongoBulkWriteError: $objectToArray requires a document input, found: string
.
How can I make the above working correctly inside a bulk operation?