我在MongoDB中有以下updateMany
个操作:
myCollection.updateMany({
_id: {
$in: myIds,
}
}, [
{
$set: {
"nested.field": {
$arrayToObject: {
$filter: {
input: {
$objectToArray: "nested.field",
},
cond: {
$gte: [
"$$this.k",
new Date().toISOString()
],
}
}
}
}
}
}
])
基本上,它所做的是过滤掉$.nested.field
内关键字小于当前日期的所有元素.
我试图将其放入批量操作(与其他操作一起),但得到一个错误(当与简单的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();
执行上面的命令时,我得到以下错误:MongoBulkWriteError: $objectToArray requires a document input, found: string
.
如何在批量操作中正确执行上述操作?