考虑以下文档集合,其中包含一系列文档:
PurchaseOrder {
id: '5b675d8180db9c13ac5b5c16',
productItems: [
{
quantity: 10,
discount_id: '5af6452dcab5ce0ee2d47e63'
},
{
quantity: 10,
discount_id: '5af6452dcab5ce0ee2d47e66'
},
{
quantity: 12,
discount_id: null
},
{
quantity: 9,
discount_id: '5af6452dcab5ce0ee2d47e63'
},
{
quantity: 7,
discount_id: '5af6452dcab5ce0ee2d47e66'
},
{
quantity: 3,
discount_id: null
},
{
quantity: 5,
discount_id: '5af6452dcab5ce0ee2d47e66'
}
]
}
我需要以编程方式使用猫鼬为所有集合更新所有discount_id's
,其中discount_id
等于某些id
(例如:5af6452dcab5ce0ee2d47e63
)。
这是我的尝试:
let selectedDiscount = {
_id: '5af6452dcab5ce0ee2d47e63'
}
PurchaseOrderModel.update(
{},
{ $set: { "productItems.$[item].discount_id": null } },
{
multi: true,
arrayFilters: [{ "item.discount_id": selectedDiscount._id }]
}
);
不幸的是,我在上述声明中遇到了错误Can't use $set
。
如何在一个猫鼬通话中实现这种操作?