【问题标题】:Getting Mongo results from only previous month avg by default默认情况下仅从上个月的平均值获取 Mongo 结果
【发布时间】:2019-02-09 07:58:35
【问题描述】:

我在 mongodb 中有这些数据:

 [
{

    "CreateDate" : ISODate("2018-08-08T04:53:51.840Z"),
    "cummulativeKWH" : 368.5,
     "frequency" : "50.12"
},
{

    "CreateDate" : ISODate("2018-08-08T04:53:51.840Z"),
    "cummulativeKWH" : 369.5,
     "frequency" : "50.12"
},
{

    "CreateDate" : ISODate("2018-09-03T04:53:51.840Z"),
    "cummulativeKWH" : 369.5,
     "frequency" : "50.12"
},
]

我是 MongoDb 的新手,希望能在此查询方面提供一些帮助。我编写了以下聚合管道。只有我使用过滤方法和选定日期时,我才需要取上个月的累积 KWH 平均值。如何获取上个月累积千瓦时的平均值(例如:8 月 1 日至 8 月 31 日),仅默认情况下没有选择且没有过滤器。
有人请给我建议。

 db.collection.aggregate([
  { "$match": {
    'createDate': {'$gte':ISODate("2018-08-01"), '$lte':ISODate("2018-08-31") } 
  }},
  {$group: {_id:null,
   cummulative: {$avg:"$cummulativeKWH"}
}}
])

我正在尝试这种聚合,我得到了输出,但是如何在没有日期选择和没有过滤器的情况下获取如何获取默认的上个月数据平均值。

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    自 3.6 版本起

    您必须从日期中提取年份和月份。 试试下面的聚合

    db['02'].aggregate(
        [
            // Stage 1
            {
                $match: {
                    $expr: {$and:[
                      {$eq:[{$year:"$CreateDate"},{$year:new Date()}]},
                      {$eq:[1,{$subtract:[{$month:new Date()},{$month:"$CreateDate"}]}]},  
                    ]}
                }
            },
    
            // Stage 2
            {
                $group: {
                _id:{year:{$year:"$CreateDate"},month:{$month:"$CreateDate"}},
                        avg : {$avg:"$cummulativeKWH"}
    
                }
            },
        ],
    );
    

    3.6 版之前 你可以使用 $redact 阶段:

    db['02'].aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $group: {
            _id:{year:{$year:"$CreateDate"},month:{$month:"$CreateDate"}},
                    avg : {$avg:"$cummulativeKWH"},       
            }
        },
    
        // Stage 2
        {
            $redact: {
              $cond: {  if: { $and:[
                {$eq: [ "$_id.year", {$year:new Date()} ]}, 
                {$eq: [-1, {$subtract:[ "$_id.month", {$month:new Date()} ]}]}
                ]},
                      then: "$$KEEP",
                      else: "$$PRUNE"
              }
            }
        },
    ],
    

    );

    结果:

    { 
        "_id" : {
            "year" : NumberInt(2018), 
            "month" : NumberInt(8)
        }, 
        "avg" : 369.0
    }
    

    【讨论】:

    • 嗨,matthPen 月平均值在那里,但是如何只获取上个月或者例如现在的 sep 月如何获取 8 月的平均值,如果有可能的话,自动获取 sep 月的平均值
    • 例如如何显示最大(月-1)平均值请任何人建议我@matthPen
    • 编辑了我的答案:-添加了 $match 阶段以仅获取相关文档,-删除了 $addField 阶段并在小组阶段添加了年月计算。请注意,此时($group 阶段),只有相关文档被传递到管道,因此按 _id:null 分组将得到相同的结果。我保持不变只是为了在结果中保留年份和月份信息。
    • 我会尝试这段代码,但会抛出 "errmsg" : "unknown top level operator: $expr",错误请解决我的问题@matthPen
    • 您使用的是哪个版本? $expr 出现在 3.6
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多