4

I've asked a question couple of days ago about updating arrays in nested arrays of objects. Right now MongoDB 3.6 officially supports it via arrayFilters feature.

Is it implemented in Mongoose 5.x.x? What's the syntax? Which method should I use?

websanya
  • 171
  • 1
  • 14

1 Answers1

4

Actually here is an example of findOneAndUpdate command:

Company.findOneAndUpdate(
  {'companyId': parseInt(req.params.companyId)},
  {$pull: {'companyDivisions.$[element].divisionDepartments': {'departmentId': parseInt(req.params.departmentId)}}},
  {arrayFilters: [{'element.divisionId': parseInt(req.params.divisionId)}]},
  (err) => {
    if (err) res.status(400).json(err)
    res.status(200).json({success: true, message: 'this worked without errors!'})
  }
)

I had two problems:

1) I tried to add a test field which wasn't represented in my schema.

2) I completely forgot to parseInt the hell out of my params, because in my schema these are numbers.

Thank you everybody. :D

websanya
  • 171
  • 1
  • 14
  • Shouldn't it be `$[element.divisionId]`? Or just `'element'` in your arrayFilters object? It is my understanding that those identifiers should match? – Adam Reis Oct 13 '20 at 21:29