如何在 mongoose 5.x.x 中使用 arrayFilters? [英] How yo use arrayFilters with mongoose 5.x.x?
问题描述
我问了一个问题 几天前关于更新嵌套对象数组中的数组.现在 MongoDB 3.6 通过 arrayFilters
功能正式支持它.
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.
是否在 Mongoose 5.x.x 中实现?语法是什么?我应该使用哪种方法?
Is it implemented in Mongoose 5.x.x? What's the syntax? Which method should I use?
推荐答案
其实这里是一个findOneAndUpdate
命令的例子:
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!'})
}
)
我有两个问题:
1) 我尝试添加一个 test
字段,该字段未在我的架构中表示.
1) I tried to add a test
field which wasn't represented in my schema.
2) 我完全忘记了 parseInt
我的参数,因为在我的架构中这些是数字.
2) I completely forgot to parseInt
the hell out of my params, because in my schema these are numbers.
谢谢大家.:D
这篇关于如何在 mongoose 5.x.x 中使用 arrayFilters?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!