0

I want to update address based on _id for the nested array object in below document:

{
  "_id": "fb369348-341b-11e9-9e05-03b09392591c",
  "user": [
    {
      "_id": "fb369349-341b-11e9-9e05-03b09392591c",
      "name": "test1",
      "address": [
        {
          "_id": "fb369350-341b-11e9-9e05-03b09392591c",
          "loc1": "place1, chennai, India",
          "type": "home"
        },
        {
          "_id": "fb369351-341b-11e9-9e05-03b09392591c",
          "loc1": "palce2, chennai, India",
          "type": "work"
        }
      ]
    },
    {},
    {},
    {}
  ]
}

I am using below query in mongodb its working fine and the record is getting updated:

db.user_details.update(
    { "user.address._id": 'fb369350-341b-11e9-9e05-03b09392591c' },
    { $set: { 
            "user.$.address.$[i].loc1": "11111111111",
            "user.$.address.$[i].type": "fdfdfddfdfdfdfdfdfdfdf" 
            } 
    },
    { arrayFilters: [{ "i._id": 'fb369350-341b-11e9-9e05-03b09392591c'}] }
)

but when I am trying to use the above query in node js I am getting the error:

TypeError: Cannot read property 'castForQuery' of undefined

Can anyone help me to understand why I am getting a castForQuery error, _id is uuid and saved as string type?

1 Answer 1

0

I faced the same problem. In my case everything was right but my mongoose version was not appropriate.

I salved my issue by updating the Mongoose version. Previously I was using [email protected]. By the following command, I upgraded my mongoose version.

npm install [email protected]  

After that, it worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.