$ push不支持猫鼬批量操作

发布于 2025-02-07 02:29:50 字数 1344 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2025-02-14 02:29:50

问题不是不支持$ push,而是您通过更新文档中的参数的方式,它被解释为顶级字段。请参阅有关字段名称注意事项,特别是修改更新的文档

您的代码updateOne({$ set:docupdates [i]})等同于以下内容:

updateOne({
  $set: {
    id: docId1,
    status: "created",
    $push: {
      updates: {
        status: "created",
        referenceId: referenceId,
        createdTimestamp: "2022-06-14T00:00:00.000Z"
      }
    }
  }
})

因为$ push ,您正在告诉MongoDB将名为$ push的字段设置为带有名为更新的字段的字段,该字段具有status /code>,<代码> ReferenceId 和CreateTimestamp字段。

您想做的是用两个不同的更新运算符指定更新文档。换句话说,$ push需要与$ set处于同一级别。

类似:

updateOne({
  $set: {
    id: docId1,
    status: "created"
  },
  $push: {
    updates: {
      status: "created",
      referenceId: referenceId,
      createdTimestamp: "2022-06-14T00:00:00.000Z"
    }
  }
})

如何在docupdates数组中表示。也许您可以做类似的事情:

let docUpdates = [
    {
        id : docId1,
        update : {
            $set : {
               status : "created"
            },
            $push : {
                updates : {
                    status: "created",
                    referenceId : referenceId,
                    createdTimestamp : "2022-06-14T00:00:00.000Z"
                }
            }
        }
    }
    // ... Other updates
]

并在循环中使用它:

bulkUpdate.find({_id : docUpdates[i].docId}).updateOne(docUpdates[i].update)

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:

updateOne({
  $set: {
    id: docId1,
    status: "created",
    $push: {
      updates: {
        status: "created",
        referenceId: referenceId,
        createdTimestamp: "2022-06-14T00:00:00.000Z"
      }
    }
  }
})

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 called updates, which has status, referenceId, and createdTimestamp 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:

updateOne({
  $set: {
    id: docId1,
    status: "created"
  },
  $push: {
    updates: {
      status: "created",
      referenceId: referenceId,
      createdTimestamp: "2022-06-14T00:00:00.000Z"
    }
  }
})

How you represent that in the docUpdates array is up to you. Maybe you can do something like:

let docUpdates = [
    {
        id : docId1,
        update : {
            $set : {
               status : "created"
            },
            $push : {
                updates : {
                    status: "created",
                    referenceId : referenceId,
                    createdTimestamp : "2022-06-14T00:00:00.000Z"
                }
            }
        }
    }
    // ... Other updates
]

And use it in the loop like:

bulkUpdate.find({_id : docUpdates[i].docId}).updateOne(docUpdates[i].update)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文