简体   繁体   English

显示金额大于 10000,其中金额数据类型为 MongoDB 中的字符串

[英]Show amount greater than 10000, where amount data type is string in MongoDB

db.transaction.aggregate(
            [
                {
                    "$match":
                    {"AMOUNT":{"$ne":null}}
                },
                {
                "$group":
                    {"_id":{}}
                },
                {
                    "$addFields":
                    {AMOUNT:{$toDouble:["$AMOUNT"]}}
                },
                {
                "$project":
                {"AMOUNT":{"$gt": 10000}}
                }
            ]
        );

Trying to fetch amount from the collection which is greater than 10000, as I'm working in MongoDB so data is in string format, so I'm using aggregation with $addFields parameter to change the string into the double and then apply the $gt function.尝试从大于 10000 的集合中获取数量,因为我在 MongoDB 中工作,所以数据是字符串格式,所以我使用带有 $addFields 参数的聚合将字符串更改为双精度然后应用 $gt功能。

Tried multiple way by arranging query in group but not able to solve it.通过在组中安排查询尝试了多种方式,但无法解决。 Please help请帮忙

db.collection.find({
  $expr: {
    $gt: [
      {
        $toDouble: "$AMOUNT"
      },
      1000
    ]
  }
})

You can use same $expr in aggregation function inside $match您可以在 $match 内的聚合函数中使用相同的 $expr

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $gt: [
          {
            $toDouble: "$AMOUNT"
          },
          1000
        ]
      }
    }
  }
])

Per @Buzz Moschetti suggestion:根据@Buzz Moschetti 的建议:

db.collection.aggregate([
  {$match: {$expr: {$gt: [{$toDouble: "$AMOUNT"}, 10000]}}}
])

Or:或者:

db.collection.find(
  {$expr: {$gt: [{$toDouble: "$AMOUNT"}, 10000]}}}
)

See how it works on the playground example看看它在操场上的例子是如何工作的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM