MongoDB. 如果值为负数,则更新任何字段。

huangapple go评论54阅读模式
英文:

MongoDb. Update any field if the value is negative

问题

我有一个包含类似以下文档的集合:

{
  "_id": "5f7db1c8e3ec502bf2f2c97c",
  "subject": "AA0001",
  "metrics": {
     "m001": 40.8,
     "m0022": 58.8,
     .....
     "m0101": -5.0
  }
}

在“metrics”内的所有字段都是数字。负数值是错误的。我想用“NA”替换任何负数值。但我有两个问题:

  • 每个文档中具有负值的字段都不同。
  • “metrics”内的字段并不总是相同的。一个文档可能比另一个文档拥有更多的“metrics”。

有什么建议吗?

英文:

I have a collection with documents like this:

{
  "_id": "5f7db1c8e3ec502bf2f2c97c",
  "subject": "AA0001",
  "metrics": {
     "m001": 40.8,
     "m0022": 58.8,
     .....
     "m0101: -5.0,
  }
}

All the fields inside "metrics" are numbers. The negative values are wrong. I want to replace any negative value with "NA". But I have two problems:

  • The fields with negative values are different for each document.
  • The fields inside "metrics" are not always the same. One document can have more "metrics" than other.

¿Any idea?

答案1

得分: 1

Sure, here's the translated code portion:

你可以使用 $objectToArray 将度量对象转换为键值对对象数组,然后使用 $map 进行转换,如果数字为负数,则使用条件操作将其替换为 "NA"。在完成转换后,你可以使用 $arrayToObject 将数组转换回对象。

db.collection.aggregate([
  {
    $addFields: {
      metrics: {
        $map: {
          input: { $objectToArray: "$metrics" },
          in: {
            k: "$$this.k",
            v: { $cond: [ { $gte: [ "$$this.v", 0 ] }, "$$this.v", "NA" ] }
          }
        }
      }
    }
  },
  {
    $addFields: {
      metrics: { $arrayToObject: "$metrics" }
    }
  }
])

你还可以更新集合以反映这些更改。从Mongo 4.2开始,你可以在 update 调用中编写聚合管道

$addFields$set 可以互换使用。

db.collection.update({},
[
  {
    $addFields: {
      metrics: {
        $map: {
          input: { $objectToArray: "$metrics" },
          in: {
            k: "$$this.k",
            v: { $cond: [ { $gte: [ "$$this.v", 0 ] }, "$$this.v", "NA" ] }
          }
        }
      }
    }
  },
  {
    $addFields: {
      metrics: { $arrayToObject: "$metrics" }
    }
  }
],
{
  multi: true
})

playground

英文:

you can use $objectToArray to convert metrics object into an array of key-value pair objects and then $map it to transform the array in a way if the number is negative it will be replaced with "NA" using a condition operation. After doing the transformation you can convert the array back to an object using $arrayToObject

db.collection.aggregate([
  {
    $addFields: {
      metrics: {
        $map: {
          input: { $objectToArray: "$metrics" },
          in: {
            k: "$$this.k",
            v: { $cond: [ { $gte: [ "$$this.v", 0 ] }, "$$this.v", "NA" ] }
          }
        }
      }
    }
  },
  {
    $addFields: {
      metrics: { $arrayToObject: "$metrics" }
    }
  }
])

playground

you can also update the collection to reflect these changes. Since Mongo 4.2 you can write aggregation pipeline inside update calls

$addFields and $set can be used interchangeably

db.collection.update({},
[
  {
    $addFields: {
      metrics: {
        $map: {
          input: { $objectToArray: "$metrics" },
          in: {
            k: "$$this.k",
            v: { $cond: [ { $gte: [ "$$this.v", 0 ] }, "$$this.v", "NA" ] }
          }
        }
      }
    }
  },
  {
    $addFields: {
      metrics: { $arrayToObject: "$metrics" }
    }
  }
],
{
  multi: true
})

playground

答案2

得分: 0

你可以使用以下代码。

db.collection.updateMany(
{ "metrics": { $exists: true } },
[
{
$set: {
"metrics": {
$arrayToObject: {
$map: {
input: { $objectToArray: "$metrics" },
in: {
k: "$$this.k",
v: {
$cond: {
if: { $lt: [ "$$this.v", 0 ] },
then: "NA",
else: "$$this.v"
}
}
}
}
}
}
}
}
]
);

英文:

You can use below code.

db.collection.updateMany(
  { "metrics": { $exists: true } },
  [
    {
      $set: {
        "metrics": {
          $arrayToObject: {
            $map: {
              input: { $objectToArray: "$metrics" },
              in: {
                k: "$$this.k",
                v: {
                  $cond: {
                    if: { $lt: [ "$$this.v", 0 ] },
                    then: "NA",
                    else: "$$this.v"
                  }
                }
              }
            }
          }
        }
      }
    }
  ]
);

huangapple
  • 本文由 发表于 2023年5月22日 22:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定