我已经在这个论坛中搜索了我的以下问题,但我无法找到解决方案。
库存收集:
{
"_id" : ObjectId("555b1978af015394d1016374"),
"Billno" : "ABC1",
"Device_id" : "strsdedgfrtg12",
"item" : [
{
"item_id" : 232,
"size" : "S",
"qty" : 25
},
{
"item_id" : 272,
"size" : "M",
"qty" : 5
}
],
"category" : "clothing"
}
inventory_new 收藏:
{
"_id" : ObjectId("555b1978af015394d1016374"),
"Billno" : "ABC1",
"Device_id" : "strsdedgfrtg12",
"item" : [
{
"item_id" : 232,
"size" : "S",
"qty" : 25
},
{
"item_id" : 272,
"size" : "M",
"qty" : 5000
}
],
"category" : "clothing"
}
现在我必须使用inventory_new collections Embedded item“qty”字段值更新库存集合嵌入式数组的项目“qty”。我试过下面的代码。但我没有成功。请指教。
db.inventory.find().forEach(function (doc1) {
var doc2 = db.inventory_copy.find({ Billno: doc1.Billno},{ Device_id: doc1.Device_id},{ item.item_id: doc1.item.item_id}, {item.qty: 1 });
if (doc2 != null) {
doc1.item.qty = doc2.item.qty;
db.inventory.save(doc1);
}
});
谢谢