我需要在2个数组中$inc数量,然后返回更新后的文档。
Mongodb文档
_id: _id ***********
quantity: [
{
"storage": "GODOWN",
"qty": 1,
"_id": "666e0f15876d5be1a8ce0a04"
},
{
"storage": "SHOP",
"qty": 0,
"_id": "666e0f15876d5be1a8ce0a05"
},
{
"storage": "GODOWN 2",
"qty": 0,
"_id": "666e0f15876d5be1a8ce0a06"
}
],
pricing: [{
"firstAddedDate": "06/16/2024",
"mrp": 0,
"price": 0,
"qty": 1,
"total": 80,
"default": true,
"_id": "666e0f15876d5be1a8ce0a03"
}]
我需要增加定价数组的对象的数量,这些对象的值为'default:true。
这是代码-
defualtPricingUpdated = await Item.findOneAndUpdate(
{ _id: newEntry.itemID, "pricing.default": true, "quantity._id": newEntry.storageID },
{ $inc: { "pricing.$.qty": Number(newEntry.qty), "quantity.$.qty": newEntry.qty, "pricing.$.total": newEntry.amount }},
{ returnDocument: "after" }
).session(session)
因此,此命令在定价数组中创建一个新条目,而不是递增现有条目。