Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aggregate group by months and match by given year

I’m trying to match by a given year (lets say 2008) and from there, group by months in order to find the count for each month in 2008. So far, im able to group by month, but im unable to filter by only a particular year. The date column is stored as a datetime object

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
     },

    }])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

$expr allows you to use any valid aggregation expression within a $match

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading