【问题标题】:Can't Aggregate in Mongoose. Arguments must be aggregate pipeline operators无法在 Mongoose 中聚合。参数必须是聚合管道运算符
【发布时间】:2021-06-16 04:34:43
【问题描述】:

我正在尝试按日期对数据(具体为 WaterConsumption)进行分组: 如果 creationDate = creationDate 我想总结 WaterConsumption。 但我有点困惑 Mongoose Aggregate 的工作原理。 我的数据:

    {
      "_id": "60538930f4bb883b0fe40f2f",
      "creationDate": "2021-02-03T17:12:04.022Z",
      "duration": 400,
      "voltage": "220",
      "avgPower": 6241,
      "maxPower": 6473,
      "WaterConsumption": 27,
      "avgTemperature": 36,
      "shower": "5fb56ce7734b7e04b9c97c9b",
      "user": "5f6cb0496a8c5a0deaa1a746",
      "__v": 0,
      "id": "60538930f4bb883b0fe40f2f"
    },
    {
      "_id": "60538bfc49a75c3ca75c0088",
      "creationDate": "2021-02-16T17:21:00.615Z",
      "duration": 900,
      "voltage": "220",
      "avgPower": 4300,
      "maxPower": 5600,
      "WaterConsumption": 32,
      "avgTemperature": 28,
      "shower": "5fb56d04734b7e04b9c97c9c",
      "user": "5f6cb0496a8c5a0deaa1a746",
      "__v": 0,
      "id": "60538bfc49a75c3ca75c0088"
    }...

我的代码:

    let dateTwo = Measure.aggregate([
        {$match: {creationDate: "$creationDate"}},
        {$group: {_id: "$id", total: {$sum: "$WaterConsumption"}}}
    ])

我可以使用 JavaScript 做到这一点,但我不知道如何使用 Mongoose 做到这一点。

【问题讨论】:

    标签: javascript mongoose


    【解决方案1】:

    我使用代码解决了我的问题:

        let dateTwo = await Measure.aggregate([
            {$match: {duration: 360}},
            {$group: {_id: "$user", total: {$sum: "$WaterConsumption"}}}
        ]);
    

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 2016-09-17
      • 2018-04-24
      • 2017-08-24
      • 2014-12-11
      • 2018-04-22
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多