[英]Meteor update multiple nested array elements
let items = [
{
name: "Choco",
id: "12344",
qty: 24,
status: "open"
},
{
name: "Mils",
id: "12346",
qty: 5,
status: "open"
},
{
name: "boom",
id: "12345",
qty: 10,
status: "open"
}
];
Given the array above, is it possible to update the document without calling a forEach on each of them. 给定上面的数组,可以在不对每个文档调用forEach的情况下更新文档。
For instance what i am currently doing is this 例如我目前正在做的是
_.each(items, function( item ) {
Orders.update({_id: doc.orderId, "items.id": item.id},
{
"$set": {
'items.$.status': item.status,
'items.$.qty': item.qty,
}
})
})
is it possible to just update the entire array at once without create a new mongo db transaction 是否可以一次更新整个阵列而无需创建新的mongo db事务
You can read all the order: 您可以阅读所有订单:
var order = Orders.findOne({_id: doc.orderId});
Than adjust order.items
with the new data and then update in a single operation: 比用新数据调整
order.items
,然后在单个操作中进行更新:
Orders.update({_id: doc.orderId },{"$set": {items:order.items}}); Orders.update({_ id:doc.orderId},{“ $ set”:{items:order.items}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.