mongoose: Array Filters broken 5.4.4

Mongoose: 5.4.4 NodeJS: LTS MongoDB: 4

UpdateMany fails when filter is in nested array

const m = {
  arr: [
    {
      id: String,
      nestedArr: [
        {
          nestedId: String,
          code: Boolean
        }
      ]
    }
  ]
};

const o = {
  _id: '0'
  arr: [
    {
      id: '1',
      nestedArr: [
        {
          nestedId: '2',
          code: false
        }
      ]
    }
  ]
};

const q = {
  _id: '0'
}

const p = {
  'arr.$[arr].nestedArr.$[nArr].code': true
}

const opts = {
  arrayFilters: [
    {
      'arr.nestedArr.nestedId': '2'
    }, {
      'nArr.nestedId': '2'
    }
  ]
}

Model.updateMany(q, p, opts)
TypeError: Cannot read property 'castForQuery' of undefined
    at castArrayFilters (/Users/dbayo/data-model/node_modules/mongoose/lib/helpers/update/castArrayFilters.js:60:37)
    at _castArrayFilters (/Users/dbayo/data-model/node_modules/mongoose/lib/query.js:1736:5)
    at model.Query._updateThunk (/Users/dbayo/data-model/node_modules/mongoose/lib/query.js:3449:3)
    at model.Query.<anonymous> (/Users/dbayo/data-model/node_modules/mongoose/lib/query.js:3546:23)
    at model.Query._wrappedThunk [as _updateMany] (/Users/dbayo/data-model/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
    at /Users/dbayo/data-model/node_modules/kareem/index.js:278:20
    at _next (/Users/dbayo/data-model/node_modules/kareem/index.js:102:16)
    at process.nextTick (/Users/dbayo/data-model/node_modules/kareem/index.js:499:38)

On the other hand, could you explain me what is the purpose of that file? lib/helpers/update/castArrayFilters.js

I don’t understand why filter path becomes to arr.0.nestedArr.code, because filter is only applying to first element.

Thank you all in advance, Best regards

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 18

Commits related to this issue

Most upvoted comments

Hi @vkarpov15

About this error. The last release v5.4.21, fix the problem (TypeError: Cannot read property ‘castForQuery’ of undefined) but now I have the next error:

Error: Could not find path "routes.0.tracks.0.lugar_origen._id" in schema at castArrayFilters (D:\ProjectsWeb\ext\bus\back\back\node_modules\mongoose\lib\helpers\update\castArrayFilters.js:61:13) at _castArrayFilters (D:\ProjectsWeb\ext\bus\back\back\node_modules\mongoose\lib\query.js:1742:5) at model.Query._updateThunk (D:\ProjectsWeb\ext\bus\back\back\node_modules\mongoose\lib\query.js:3455:3) at model.Query.<anonymous> (D:\ProjectsWeb\ext\bus\back\back\node_modules\mongoose\lib\query.js:3552:23) at model.Query._wrappedThunk [as _updateMany] (D:\ProjectsWeb\ext\bus\back\back\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)

In befores versions the code was working well. Maybe have i to change the code for use array filters???

this is my code

CompanyModel.updateMany( { _id: companyId }, { $set: { "routes.$[].tracks.$[track].lugar_origen": oriPlace } }, { arrayFilters: [{ "track.lugar_origen._id": idPlaceToEdit }] }, function(err, company) { if (err) console.log(err); else console.log(company); } );

Thanks for your patience. Fix will be in v5.4.21

Here is another report of failures in version 5.4.9.

Yes, i have similar error when i use arrayFilters in 5.4.6.