How yo use arrayFilters with mongoose 5.x.x? [duplicate]

若如初见. 提交于 2020-07-18 02:10:03

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/49095532/how-yo-use-arrayfilters-with-mongoose-5-x-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!