简体   繁体   English

MongoDB 查询对大于和小于某个值的记录进行分组和计数

[英]MongoDB query to group and count records greater than and smaller than a certain value

Could you please help in correcting this query to group and count Continente based on PIB value greater than and smaller than 1.3?您能否帮助更正此查询以根据大于和小于 1.3 的PIB值对Continente进行分组和计数?

db.collection.aggregate([
  {
    $project:{
      "item":1,
      "PIB":1
    }
  },
  {
    $addFields:{
      moreThan1.3:{
        $cond:[
          {
            $gt:[
              "$PIB",
              1.3
            ]
          },
          1,
          0
        ]
      },
      lessThan1.3:{
        $cond:[
          {
            $lte:[
              "$PIB",
              1.3
            ]
          },
          1,
          0
        ]
      }
    }
  },
  {
    $group:{
      "_id":"$Continente",
      "countSmaller":{
        $sum:"$lessThan1.3"
      },
      "countBigger":{
        $sum:"$moreThan1.3"
      }
    }
  }
]).pretty()

This is the database in Robo 3T/MongoDB:这是 Robo 3T/MongoDB 中的数据库: MongoDB中的数据库

This error is appearing:出现此错误:

Error: Line 10: Unexpected number错误:第 10 行:意外数字

Thank you!谢谢!

Try this aggregation query:试试这个聚合查询:

db.collection.aggregate([
  {
    $group: {
      _id: "$continente",
      /** counting docs based on condition, for an on-going doc if condition is met pass in 1 or 0 */
      countSmaller: { $sum: { $cond: [{ $lte: ["$PIB", 1.3] }, 1, 0] } },
      countBigger: { $sum: { $cond: [{ $gt: ["$PIB", 1.3] }, 1, 0] } }
    }
  }
]);

Test: mongoplayground测试: mongoplayground

I have updated the query, you were close, I used $addFields to store the field.我已经更新了查询,你很接近,我使用$addFields来存储该字段。

db.collection.aggregate([
  {
    $project:{
      "item":1,
      "PIB":1
    }
  },
  {
    $addFields:{
      moreThan10:{
        $cond:[
          {
            $gt:[
              "$PIB",
              10
            ]
          },
          1,
          0
        ]
      },
      lessThan10:{
        $cond:[
          {
            $lte:[
              "$PIB",
              10
            ]
          },
          1,
          0
        ]
      }
    }
  },
  {
    $group:{
      "_id":"$Continente",
      "countSmaller":{
        $sum:"$lessThan10"
      },
      "countBigger":{
        $sum:"$moreThan10"
      }
    }
  }
]).pretty()

Hope this will help:)希望这会有所帮助:)

Update更新

db.collection.aggregate([
  {
    $project:{
      "item":1,
      "PIB":1
    }
  },
  {
    $addFields:{
      "moreThan1.3":{
        $cond:[
          {
            $gt:[
              "$PIB",
              1.3
            ]
          },
          1,
          0
        ]
      },
      "lessThan1.3":{
        $cond:[
          {
            $lte:[
              "$PIB",
              1.3
            ]
          },
          1,
          0
        ]
      }
    }
  },
  {
    $group:{
      "_id":"$Continente",
      "countSmaller":{
        $sum:"$lessThan1.3"
      },
      "countBigger":{
        $sum:"$moreThan1.3"
      }
    }
  }
]).pretty()

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

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