How do I use aggregation operators in a $match in MongoDB (for example $year or $dayOfMonth)?

前端 未结 3 1155
生来不讨喜
生来不讨喜 2021-02-12 16:22

I have a collection full of documents with a created_date attribute. I\'d like to send these documents through an aggregation pipeline to do some work on them. Ideally I would

3条回答
  •  遇见更好的自我
    2021-02-12 16:42

    As you already found, you cannot $match on fields that are not in the document (it works exactly the same way that find works) and if you use $project first then you will lose the ability to use indexes.

    What you can do instead is combine your efforts as follows:

    {
        aggregate: 'posts',
        pipeline: [
             {$match: {
                 created_date : 
                      {$gte:{$date:'2012-09-01T04:00:00Z'}, 
                      $lt:  {date:'2012-10-01T04:00:00Z'} 
                      }}
                 }
             },
             {$group:
                 {_id: '0',
                  totalComments:{$sum:'$comments'}
                 }
              }
        ]
     }
    

    The above only gives you aggregation for September, if you wanted to aggregate for multiple months, you can for example:

    {
        aggregate: 'posts',
        pipeline: [
             {$match: {
                 created_date : 
                      { $gte:'2012-07-01T04:00:00Z', 
                        $lt: '2012-10-01T04:00:00Z'
                      }
             },
             {$project: {
                  comments: 1,
                  new_created: {
                            "yr" : {"$year" : "$created_date"},
                            "mo" : {"$month" : "$created_date"}
                         }
                  }
             },
             {$group:
                 {_id: "$new_created",
                  totalComments:{$sum:'$comments'}
                 }
              }
        ]
     }
    

    and you'll get back something like:

    {
        "result" : [
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 7
                },
                "totalComments" : 5
            },
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 8
                },
                "totalComments" : 19
            },
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 9
                },
                "totalComments" : 21
            }
        ],
        "ok" : 1
    }
    

提交回复
热议问题