大家好,我有以下文件。
{
"_id" : ObjectId("568d5db6809af7688306901a"),
"name" : "name",
"market" : "market",
"state" : "state",
"products" : [
{
"updated" : ISODate("2016-11-11T23:07:11.114Z"),
"quantity" : 9,
"product" : "Prouct1",
"_id" : ObjectId("58264bd882547072b855547f")
},
{
"updated" : ISODate("2016-11-11T23:02:26.174Z"),
"quantity" : 10,
"product" : "Prouct2",
"_id" : ObjectId("58264bd882547072b855546c")
},
{
"updated" : ISODate("2016-11-11T23:02:26.174Z"),
"quantity" : 10,
"product" : "Prouct3",
"_id" : ObjectId("58264bd882547072b855548a")
},
{
"updated" : ISODate("2016-11-11T23:02:26.174Z"),
"quantity" : 10,
"product" : "Prouct4",
"_id" : ObjectId("58264bd882547072b855576a")
},
{
"updated" : ISODate("2016-11-11T22:58:14.083Z"),
"quantity" : 10,
"product" : "Prouct5",
"_id" : ObjectId("58264bd882547072b855547f")
},
{
"updated" : ISODate("2016-11-11T23:07:11.109Z"),
"quantity" : -1,
"product" : "Prouct5",
"_id" : ObjectId("58264f1fa534b62abc89edf8")
},
{
"updated" : ISODate("2016-11-11T23:07:11.119Z"),
"quantity" : -1,
"product" : "Prouct7",
"_id" : ObjectId("58264f1fa534b62abc89edfa")
}
]
}
以下是我的查询。
query = {'products.product': {$in: ['Prouct1', 'Prouct2', 'Prouct3']}};
但以下更新仅更新Prouct1。如何更改我的更新以更新Prouct1,Prouct2和Prouct3?
update = {$inc: {'products.$.quantity': -1},'products.$.updated': now};
一如既往地感谢您的帮助和建议。
答案 0 :(得分:2)
您无法使用简单的查找查询来执行这些操作。 解决方法
{$inc: {'products.0.quantity': -1,'products.1.quantity': -1,'products.2.quantity': -1}}
或者
您可以使用汇总框架$unwind
您的阵列,$match
documnet,更新documnets,再次$push
将其更新为数组