【问题标题】:Aggregate group by months and match by given year按月聚合组并按给定年份匹配
【发布时间】:2021-12-30 09:30:17
【问题描述】:

我正在尝试按给定年份(比如说 2008 年)进行匹配,然后从那里按月分组,以便找到 2008 年每个月的计数。到目前为止,我能够按月分组,但我无法仅按特定年份过滤。 date 列存储为日期时间对象

database.coll.aggregate([
        
        # some expression that will only select year e.g. 2018
        {$match : {}},
    
     {$group : {
         _"id" : {$month : $date},   # group by year
         "total" : {$sum : 1}          # count values in each group
     },

    }])

【问题讨论】:

  • {$match: {$expr: {$eq: [2008, {$year: "$date"}] } } }

标签: mongodb aggregate pymongo


【解决方案1】:

{$match: {$expr: {$eq: [2008, {$year: "$date"}] } } }

$expr 允许您在$match 中使用任何有效的聚合表达式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多