Could not find path in schema while using arrayfilters in mongoose

Viewed 499

This is my shema in mongoose

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)

Here is my code for the following task

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
    });
})

Here I am trying to update the value field in objective_group. To achieve this I am using arrayfilter. But I am getting this Error

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

When I am mongoose v6 it's showing this error. on mongoose v5 I am not getting any errors but the operation is not succeeding

There is the possibility that I am not using arrayfilters in the right way because inside objective_group I am storing objects inside the array.

I am new to StackOverflow so sorry for some mistakes

1 Answers

You just have a slight error with the path you're providing, the error means that the path you gave resolved into a none array value.

You just need to change your update part from:

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

To:

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

Like so:

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

Mongo Playground

Related