如果pmd
为ed
,我希望查询更新所有DEF
数组的isActive
和true
,则可从Mongo 3.6开始使用位置运算符。
有人可以建议与Mongo 3.2中的位置运算符等效的东西吗?
文档:
{
"_id" : {
"_id" : "123456789",
"globalId" : "00012345001"
},
"regionalAttributes" : {
"CAD" : {
"priceS" : "XYZ",
"mdClusters" : {
"DEF" : [
{
"cId" : "129",
"cName" : "815",
"pmd" : ISODate("2018-01-04T00:00:00.000Z"),
"ed" : ISODate("2018-04-15T00:00:00.000Z"),
"isActive" : false
},
{
"cId" : "193",
"cName" : "811",
"pmd" : ISODate("2019-06-29T00:00:00.000Z"),
"ed" : ISODate("2019-05-30T00:00:00.000Z"),
"isActive" : true
},
{
"cId" : "194",
"cName" : "812",
"pmd" : ISODate("2018-01-04T00:00:00.000Z"),
"ed" : ISODate("2018-04-15T00:00:00.000Z"),
"isActive" : true
}
]
}
}
}
}
我尝试了elemMatch,但它仅更新第一个匹配的数组
db.getCollection('items').updateMany(
{ "_id._id" : "123456789",{ "regionalAttributes.CAD.mdClusters.DEF": { "$elemMatch": {"isActive": true }} }},
{"$set" : { "regionalAttributes.CAD.mdClusters.DEF.$.pmd" : ISODate("2019-05-29T00:00:00.000Z") ,
"regionalAttributes.CAD.mdClusters.DEF.$.ed" : ISODate("2019-06-26T00:00:00.000Z")}});