如何在 Mongoose 上使用 create()/insertMany() 检索失败的特定记录

How to retrieve the specific records that failed using create()/insertMany() on Mongoose

提问人:Kkura 提问时间:11/17/2023 最后编辑:Kkura 更新时间:11/17/2023 访问量:22

问:

我正在尝试检索因非 WriteErrors 而失败的特定记录。我试过使用 和 .我也试过用或寻找,但仍然没有成功db.collection.create(req.body, {ordered: false})db.collection.insertMany(req.body, {ordered: false}}aggregateErrors: truerawResult: true

对于 WriteErrors,即重复键,我找到了显示插入失败的实际记录的“errmsg”和“op”

"writeErrors": [{
    "err": {
        "index": 0,
        "code": 11000,
        "errmsg": "E11000 duplicate key error collection: test.collection index: _id_ dup key: { _id: \"0000010\" }",
        "op": {
            "system": "Dev",
            "user": "00000980",
            "item": "0010",
            ...
        }
    }
}]

我正在寻找一种方法来获得类似的东西,但针对 ValidationErrors,即不正确的键名、错误的数据类型等。 到目前为止,我只能捕获错误消息和路径/值,但我还没有找到类似于 WriteErrors 中的“op”的东西

{
    "errors": {
        "system": {
            "name": "ValidatorError",
            "message": "QA is not a valid system",
            "properties": {
                "message": "QA is not a valid system",
                "type": "user defined",
                "path": "system",
                "value": "QA"
            },
            "kind": "user defined",
            "path": "system",
            "value": "QA"
        }
    },
    "_message": "collection validation failed",
    "name": "ValidationError",
    "message": "collection validation failed: system: QA is not a valid system"
}

有没有办法获取错误消息 + ValidationErrors/non-WriteErrors 失败的特定记录?

提前致谢!

JavaScript 节点 .js mongodb 猫鼬

评论


答: 暂无答案