查询,以使用每个平均资产的平均值计算在mongodb中存储的平均值的平均值

时间:2020-10-27 17:57:51

标签: mongodb mongodb-query

{
"_id" : ObjectId("5f92b9efb7895affcd55ab8c"),
"active" : true,
"assetData" : {
    "asset1" : {
        "average" : 4,
        "count" : 2,
        "dataPerHour" : {
            "0" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "1" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "2" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            }
        },
        "max" : 0,
        "min" : 0
    },
    "asset2" : {
        "average" : 0,
        "count" : 0,
        "dataPerHour" : {
            "0" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "1" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "2" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            }
        },
        "max" : 0,
        "min" : 0
    },
    "asset3" : {
        "average" : 0,
        "count" : 0,
        "dataPerHour" : {
            "0" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "1" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            },
            "2" : {
                "average" : 0,
                "count" : 0,
                "max" : 0,
                "min" : 0,
                "values" : [ 
                    {
                        "time" : "2020-10-23T11:08:38.928Z",
                        "value" : 0
                    }
                ]
            }
        },
        "max" : 0,
        "min" : 0
    }
},
"average" : 4,
"count" : 2,
"date" : "2020-10-23T11:08:38.928Z",
"id" : "string",
"max" : 0,
"min" : 0,
"parameterId" : "string",
"parameterName" : "string",
"parameterValue" : 0,
"serviceId" : "string"

} ** 假设我们有多种格式的数据

,我们需要找到资产内部已存储平均值的所有平均值的平均值。

我们需要根据公式找到平均值的平均值(过滤后,就像我只需要资产1的平均值一样):

(average1count1)+(avarege2count2)+(average3 * count3)/(count1 + count2 + count3)**

1 个答案:

答案 0 :(得分:0)

您可以使用以下查询:

db.collection.aggregate([
  {
    "$group": {
      "_id": null,
      "data": {
        "$push": "$assetData.asset1"
      }
    }
  },
  {
    "$project": {
      "average": {
        "$divide": [
          {
            "$reduce": {
              "input": "$data",
              "initialValue": 0,
              "in": {
                "$add": [
                  "$$value",
                  {
                    "$multiply": [
                      "$$this.count",
                      "$$this.average"
                    ]
                  }
                ]
              }
            }
          },
          {
            $reduce: {
              input: "$data",
              initialValue: 0,
              in: {
                "$add": [
                  "$$value",
                  "$$this.count"
                ]
              }
            }
          }
        ]
      }
    }
  }
])

MongoDB Playground