假设我具有以下结构:
{
"_id" : ObjectId("5c87cbf35ed912737d097301"),
"el" : [
{
"a" : "b"
},
{
"a" : "b"
},
{
"a" : "b"
},
{
"a" : "b"
}
]
}
我如何添加到el {x:1}的每个元素?
答案 0 :(得分:1)
要更新数据库,您需要$ positional all operator
SELECT [ProductionPlanId], SUM([QtyCompleted])
FROM [CompletedPrinted]
WHERE DocumentNo = '0000027084' AND ProductionPlanItemID='5865406'
GROUP BY [ProductionPlanId];
要在查询时添加此值,您需要$addFields和$map
db.col.update({ "_id" : ObjectId("5c87cbf35ed912737d097301") }, { $set: { "el.$[].x": 1 } })