I've searched in this forum for my below issue and I'm not able to find solution.
{
"_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 collection:
{
"_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"
}
Now I've to update the inventory collection embedded array's item "qty" with inventory_new collections embedded item "qty" field value.. I've tried below code.. But I'm not succeed. Please advice.
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);
}
});
Thanks
Try the following update:
db.inventory.find().forEach(function (doc1) {
var doc2 = db.inventory_copy.findOne(
{
"Billno": doc1.Billno,
"Device_id": doc1.Device_id,
"item.item_id": doc1.item.item_id
}
);
if (doc2 != null) {
doc1.item = doc2.item;
db.inventory.save(doc1);
}
});