Node.js 如何在mongodb中获取图的条件平均数据?

Node.js 如何在mongodb中获取图的条件平均数据?,node.js,mongodb,mongoose,mongodb-query,aggregate,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregate,数据1: 数据2: { "_id" : "5eb922b4c019811689c8f8e3", "createdAt" : "2020-05-10T19:30:00.000Z", "isManual" : false, "value" : 0.66 } 数据3: { "_id" : "5eb922b4c019811689c8f8e3", "createdAt" : "2020-05-10T19:30:00.000Z", "isManual

数据1:

数据2:

{
    "_id" : "5eb922b4c019811689c8f8e3",
    "createdAt" : "2020-05-10T19:30:00.000Z",
    "isManual" : false,
    "value" : 0.66
}
数据3:

{
    "_id" : "5eb922b4c019811689c8f8e3",
    "createdAt" : "2020-05-10T19:30:00.000Z",
    "isManual" : false,
    "value" : 0.52
}
现在我需要生成一个查询,以获得值的平均值。考虑<强>手动> /强> 平均值的关键值:

    {
        "_id" : "5eb922b4c019811689c8f8e3",
        "createdAt" : "2020-05-10T19:30:00.000Z",
        "isManual" : true,
        "value" : 0.34
    }
您可以使用随来计算
total_字段
,并根据
isManual
条件使用

您可以使用随来计算
total_字段
,并根据
isManual
条件使用


您可以尝试以下聚合查询:

[
  {
    $group: {
      _id: null,
      count: {
        $sum: 1
      },
      manual: {
        $sum: {
          $cond: [
            "$isManual",
            "$value", // add $value when $isManual is true
            0
          ]
        }
      },
      not_manual: {
        $sum: {
          $cond: [
            "$isManual",
            0,
            "$value" // add $value when $isManual is false
          ]
        }
      }
    }
  },
  {
    $project: {
      _id: false,
      total_fields: "$count",
      manual_avg: {
        $divide: [
          "$manual",
          "$count"
        ]
      },
      not_manual_avg: {
        $divide: [
          "$not_manual",
          "$count"
        ]
      } 
    }
  }
]

测试:

您可以尝试以下聚合查询:

[
  {
    $group: {
      _id: null,
      count: {
        $sum: 1
      },
      manual: {
        $sum: {
          $cond: [
            "$isManual",
            "$value", // add $value when $isManual is true
            0
          ]
        }
      },
      not_manual: {
        $sum: {
          $cond: [
            "$isManual",
            0,
            "$value" // add $value when $isManual is false
          ]
        }
      }
    }
  },
  {
    $project: {
      _id: false,
      total_fields: "$count",
      manual_avg: {
        $divide: [
          "$manual",
          "$count"
        ]
      },
      not_manual_avg: {
        $divide: [
          "$not_manual",
          "$count"
        ]
      } 
    }
  }
]

测试:

您是否试图找到答案?如果是,请包括您尝试过的内容。另外,(0.66+0.52)/3=0.39,而不是0.34您是否尝试过找到答案?如果是,请包括您尝试过的内容。另外(0.66+0.52)/3=0.39,而不是0.34
db.collection.aggregate([
    /** group all docs in collection */
    {
      $group: {
        _id: null,
        total_fields: { $sum: 1 }, /** count total no.of docs */
        manual_avg: { $avg: { $cond: [ "$isManual", 0, "$value" ] } }, /** If 'isManual' is true pass-in 0 else actual value to average */
        not_manual_avg: { $avg: { $cond: [ "$isManual", "$value", 0 ] } }
      }
    },
    /** Optional stage */
    {
      $project: { _id: 0,total_fields: 1, manual_avg: { $trunc: [ "$manual_avg", 2 ] }, not_manual_avg: { $trunc: [ "$not_manual_avg", 2 ] }
      }
    }
  ])