$ push不支持猫鼬批量操作
我正在使用Mongoose中的批量操作以并发方式更新多个文档。
更新数据如下所示:
let docUpdates = [
{
id : docId1,
status : "created",
$push : {
updates : {
status: "created",
referenceId : referenceId,
createdTimestamp : "2022-06-14T00:00:00.000Z"
}
}
},
{
id : docId2,
status : "completed",
$push : {
updates : {
status: "compelted",
referenceId : referenceId,
createdTimestamp : "2022-06-14T00:00:00.000Z"
}
}
},
];
// Update documents using bulk operation
let bulkUpdate = Model.collection.initializeOrderedBulkOp();
for (let i = 0; i < docUpdates.length; i++) {
bulkUpdate.find({_id : docUpdates[i].docId}).updateOne({ $set : docUpdates[i]})
}
当我执行上述片段时,我会收到以下错误。
(node:468) UnhandledPromiseRejectionWarning: MongoBulkWriteError: The dollar ($) prefixed field '$push' in '$push' is not allowed in the context of an update's replacement document. Consider using an aggregation pipeline with $replaceWith.
at OrderedBulkOperation.handleWriteError (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/bulk/common.js:884:22)
您能帮助您找到解决此错误的工作。
谢谢,
KK
I am using the bulk operation in mongoose to update multiple documents in a concurrent manner.
The update data looks as below:
let docUpdates = [
{
id : docId1,
status : "created",
$push : {
updates : {
status: "created",
referenceId : referenceId,
createdTimestamp : "2022-06-14T00:00:00.000Z"
}
}
},
{
id : docId2,
status : "completed",
$push : {
updates : {
status: "compelted",
referenceId : referenceId,
createdTimestamp : "2022-06-14T00:00:00.000Z"
}
}
},
];
// Update documents using bulk operation
let bulkUpdate = Model.collection.initializeOrderedBulkOp();
for (let i = 0; i < docUpdates.length; i++) {
bulkUpdate.find({_id : docUpdates[i].docId}).updateOne({ $set : docUpdates[i]})
}
When I execute the above snippet, I get the below error.
(node:468) UnhandledPromiseRejectionWarning: MongoBulkWriteError: The dollar ($) prefixed field '$push' in '$push' is not allowed in the context of an update's replacement document. Consider using an aggregation pipeline with $replaceWith.
at OrderedBulkOperation.handleWriteError (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/bulk/common.js:884:22)
Could you please help in finding a work around for this error.
Thank you,
KK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是不支持
$ push
,而是您通过更新文档中的参数的方式,它被解释为顶级字段。请参阅有关字段名称注意事项,特别是修改更新的文档。您的代码
updateOne({$ set:docupdates [i]})
等同于以下内容:因为
$ push
,您正在告诉MongoDB将名为$ push
的字段设置为带有名为更新
的字段的字段,该字段具有status
/code>,<代码> ReferenceId 和CreateTimestamp
字段。您想做的是用两个不同的更新运算符指定更新文档。换句话说,
$ push
需要与$ set
处于同一级别。类似:
如何在
docupdates
数组中表示。也许您可以做类似的事情:并在循环中使用它:
The problem isn't that
$push
isn't supported, it's that the way you pass the arguments in the update document, it's interpreted as a top-level field. See the documentation about field name considerations, specifically the section on document modifying updates.This section of your code
updateOne({ $set : docUpdates[i]})
is equivalent to the following:Because the
$push
is contained in the$set
, you're telling MongoDB to set the value of a field named$push
to a document with a field calledupdates
, which hasstatus
,referenceId
, andcreatedTimestamp
fields.What you want to do instead, is specify an update document with two different update operators. In other words,
$push
needs to be on the same level as the$set
.Something like:
How you represent that in the
docUpdates
array is up to you. Maybe you can do something like:And use it in the loop like: