在猫鼬中使用数组过滤器时无法在架构中找到路径

问题描述 投票:0回答:2

这是我在猫鼬中的谢玛

import {Schema, model} from 'mongoose'



const EmployeeSchema = new Schema({
    employeeCode: {
        type: String,
    },
    name: {
        type: String,

    },
    appraisal: {
        status: {
            type: String,
            enum: ["not started", "in progress", "completed", "not started", "self rating"],
            default: "not started"
        },
        objective_group: [{
            name: {
                type: Schema.Types.ObjectId,
                ref: "ObjectiveGroup",
                required: true
            },
            value: {
                type: Number,
            },
            objective_type: [{
                name: {
                    type: Schema.Types.ObjectId,
                    ref: "ObjectiveType",
                    required: true
                },
                value: {
                    type: Number,
                },

                objective_description: [{
                    name: {
                        type: Schema.Types.ObjectId,
                        ref: "ObjectiveDescription",
                        required: true
                    },
                    value: {
                        type: Number,
                    },
                    ratings: {
                        type: {
                            type: Schema.Types.ObjectId,
                            ref: "Ratings",

                        }
                    },

                }],
                training_recommendation: {
                    type: Schema.Types.ObjectId,
                    ref: 'TrainingRecommendation'
                },
                other_recommendation: {
                    type: Schema.Types.ObjectId,
                    ref: 'OtherRecommendation'
                },
            }],
        }],
    },
})

export default model('Employee', EmployeeSchema)

这是我执行以下任务的代码

const updateValue = asyncHandler(async (req: Request, res: Response) => {

    const {id, rating} = req.body

    const employee = await Employee.findOneAndUpdate({
            "_id": "6204935ebca89023952f2da9",
            "appraisal.objective_group._id": "6207ec6a8bfc1226d3f36fb1"
        },
    {
            $set: {
                "appraisal.$[objective_group].value": 1234
            }
        },
        {

            arrayFilters: [
                {
                    'objective_group._id':  new mongoose.Types.ObjectId("6207ec6a8bfc1226d3f36fb1")
                }
            ]
        }
    )
    res.status(StatusCodes.OK).json({
        employee
    });
})

这里我尝试更新objective_group中的值字段。为了实现这一点,我使用 arrayfilter。但我收到这个错误

Error: Could not find path "appraisal.0._id" in schema

当我使用 mongoose v6 时,它显示此错误。在 mongoose v5 上我没有收到任何错误,但操作没有成功

我可能没有以正确的方式使用数组过滤器,因为在 Objective_group 中我将对象存储在数组中。

我是 StackOverflow 的新手,对于一些错误深表歉意

node.js mongodb mongoose aggregation-framework mongoose-schema
2个回答
0
投票

您提供的路径只是有一个轻微的错误,该错误意味着您提供的路径已解析为无数组值。

您只需将更新部分更改为:

"appraisal.$[objective_group].value": 1234

致:

"appraisal.objective_group.$[objective_group].value": 1234

像这样:

db.collection.update({
  "_id": "6204935ebca89023952f2da9",
  "appraisal.objective_group._id": ObjectId("6207ec6a8bfc1226d3f36fb1")
},
{
  $set: {
    "appraisal.objective_group.$[group].value": 1234
  }
},
{
  arrayFilters: [
    {
      "group._id": ObjectId("6207ec6a8bfc1226d3f36fb1")
    }
  ]
})

蒙戈游乐场


0
投票

在 2024 年,如果有人尽管有正确的查询但仍然看到此错误,我找到了原因。这是 Mongoose 中的一个错误,似乎在 6.12.x 或更高版本中已修复。

Could not find path "appraisal.0._id" in schema

在我们的代码中,

package_lock.json
文件使用了
6.7.0
,这是可重现的。将其升级到
6.12.3
为我们解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.