MongoDB-$lte 2个密钥在同一对象中相互对抗
样本数据:MongoDB-$lte 2个密钥在同一对象中相互对抗,mongodb,mongoose,mongodb-query,Mongodb,Mongoose,Mongodb Query,样本数据: let arrObj = [ { "id": 9999, "arrObj": [ { "name": "xyz", "arrId": 1, "total1": 54, "total2": 55 }, { &quo
let arrObj = [
{
"id": 9999,
"arrObj": [
{
"name": "xyz",
"arrId": 1,
"total1": 54,
"total2": 55
},
{
"type": "abc",
"arrId": 2,
"total1": 35,
"total2": 32
}
]
}
]
正在查找mongo更新,如:
db.coll('collectionName').update({
id: 9999,
arrObj.arrId: 1,
arrObj.total1 : {$lte : arrObj.total2}
},{$inc : {total2 : 1}})
我需要匹配两个字段,并在此基础上增加一个字段值。提前感谢。您可以从MongoDB v4.2开始使用
,如果找到匹配项,请检查您的条件,然后将一个添加到$map
total2
db.coll('collectionName').update(
{ id: 9999 },
[{
$set: {
arrObj: {
$map: {
input: "$arrObj",
in: {
$mergeObjects: [
"$$this",
{
$cond: [
{
$and: [
{ $eq: ["$$this.arrId", 1] },
{ $lte: ["$$this.total1", "$$this.total2"] }
]
},
{ total2: { $add: ["$$this.total2", 1] } },
"$$this"
]
}
]
}
}
}
}
}]
)