执行汇总时,是否可以对多个文档的结果进行分组

时间:2019-03-29 15:22:53

标签: mongodb mongodb-query aggregation-framework

我是mongo的新手,正在尝试执行聚合查询以计算给定文档的时间戳的最小/最大。

示例文档如下-

{
"_id" : ObjectId("5c9cd93adddca9ebb2b3fcba"),
"frequency" : 5,
"s_id" : "30081993",
"timestamp" : NumberLong(1546300800000),
"date" : ISODate("2019-01-01T00:00:00.000Z"),

"values" : {
    "1547439900000" : {
        "number_of_values" : 3,
        "min_value" : 32.13,
        "max_value" : 81.42
    },
    "1547440200000" : {
        "number_of_values" : 3,
        "min_value" : 48.08,
        "max_value" : 84.52
    },
    "1547440500000" : {
        "number_of_values" : 2,
        "min_value" : 27.39,
        "max_value" : 94.64
    }
  }
}

{
"_id" : ObjectId("5c9cd851dddca9ebb2b3f2ac"),
"frequency" : 5,
"s_id" : "27061995",
"timestamp" : NumberLong(1546300800000),
"date" : ISODate("2019-01-01T00:00:00.000Z"),

"values" : {
    "1547539900000" : {
        "number_of_values" : 31,
        "min_value" : 322.13,
        "max_value" : 831.42
    },
    "1547540200000" : {
        "number_of_values" : 3,
        "min_value" : 418.08,
        "max_value" : 8114.52
    },
    "1547740500000" : {
        "number_of_values" : 2,
        "min_value" : 207.39,
        "max_value" : 940.64
    }
  }
}

我提出了以下查询,该查询适用于单个文档。

    db.testdb.aggregate([
      {
        $match: {
          "s_id": "30081993",
          "frequency": 5,

        }
      },
      {
        $project: {
          _id: 1,
          valuesarray: {
            $objectToArray: "$values"
          }
        }
      },
      {
        $unwind: "$valuesarray"
      },
      {
        $group: {
          "_id": "",
          "min_timestamp": {
            $min: "$valuesarray.k"
          },
          "max_timestamp": {
            $max: "$valuesarray.k"
          }
        }
      }
    ]);

输出低于

{
    "_id" : "",
    "min_timestamp" : "1547439900000",
    "max_timestamp" : "1547440500000"
}

我想要一个聚合查询,该查询可以计算时间戳的最大/最小值,但是对于多个文档,即我想在 $ match 阶段使用 $ in 运算符,获得所有s_id的最小值/最大值。这可能吗?

预期:

{
    "_id" : "30081993",
    "min_timestamp" : "1547439900000",
    "max_timestamp" : "1547440500000"
}


{
    "_id" : "27061995",
    "min_timestamp" : "1547539900000",
    "max_timestamp" : "1547740500000"
}

1 个答案:

答案 0 :(得分:1)

是的,只需要进行很小的更改就可以对多个文档进行这项工作。

$match阶段,指定您的$in查询:

$match: {
    "s_id": { $in : [ "30081993", "27061995" ] }, 
    "frequency": 5,
}

$project阶段,将s_id重命名为_id,以确保我们将s_id与每个文档关联:

$project: {
    _id: "$s_id",
    valuesarray: {
        $objectToArray: "$values"
    }
}

$group阶段中,按_id分组(最初是s_id),以确保在计算$min / $max之前将时间戳正确分组在一起: / p>

$group: {
    "_id": "$_id",
    "min_timestamp": {
        $min: "$valuesarray.k"
    },
    "max_timestamp": {
        $max: "$valuesarray.k"
    }
}

整个管道:

db.testdb.aggregate([
  {
    $match: {
      "s_id": { $in : [ "30081993", "27061995" ] }, 
      "frequency": 5,
    }
  },
  {
    $project: {
      _id: "$s_id",
      valuesarray: {
        $objectToArray: "$values"
      }
    }
  },
  {
    $unwind: "$valuesarray"
  },
  {
    $group: {
      "_id": "$_id",
      "min_timestamp": {
        $min: "$valuesarray.k"
      },
      "max_timestamp": {
        $max: "$valuesarray.k"
      }
    }
  }
]);