如何在MongoDB中的$ match中使用聚合运算符(例如$ year或$ dayOfMonth)?

Mas*_*son 16 mongodb aggregation-framework

我有一个包含created_date属性的文档集合.我想通过聚合管道发送这些文档来对它们进行一些工作.理想情况下,我想在我对其进行任何其他工作之前使用$ match过滤它们以便我可以利用索引但是我无法弄清楚如何使用我的新$ year/$ month/$ dayOfMonth运算符$ match表达式.

在$ project操作中如何使用运算符有一些例子,但是我担心通过将$ project放在我的管道中的第一步然后我就失去了对索引的访问权限(MongoDB文档表明第一个表达式必须是$ match才能利用索引).

样本数据:

{
    post_body: 'This is the body of test post 1',
    created_date: ISODate('2012-09-29T05:23:41Z')
    comments: 48
}
{
    post_body: 'This is the body of test post 2',
    created_date: ISODate('2012-09-24T12:34:13Z')
    comments: 10
}
{
    post_body: 'This is the body of test post 3',
    created_date: ISODate('2012-08-16T12:34:13Z')
    comments: 10
}
Run Code Online (Sandbox Code Playgroud)

我想通过聚合管道来运行它,以获得9月份所有帖子的总评论

{
    aggregate: 'posts',
    pipeline: [
         {$match:
             /*Can I use the $year/$month operators here to match Sept 2012?
             $year:created_date : 2012,
             $month:created_date : 9
             */
             /*or does this have to be 
             created_date : 
                  {$gte:{$date:'2012-09-01T04:00:00Z'}, 
                  $lt: {$date:'2012-10-01T04:00:00Z'} }
             */
         },
         {$group:
             {_id: '0',
              totalComments:{$sum:'$comments'}
             }
          }
    ]
 }
Run Code Online (Sandbox Code Playgroud)

这可以工作,但匹配失去了对更复杂查询的任何索引的访问权限:

{
    aggregate: 'posts',
    pipeline: [
         {$project:
              {
                   month : {$month:'$created_date'},
                   year : {$year:'$created_date'}
              }
         },
         {$match:
              {
                   month:9,
                   year: 2012
               }
         },
         {$group:
             {_id: '0',
              totalComments:{$sum:'$comments'}
             }
          }
    ]
 }
Run Code Online (Sandbox Code Playgroud)

Asy*_*sky 17

正如您已经发现的那样,您不能在文档中没有的字段上匹配(它的工作方式与查找工作完全相同),如果您先使用$ project,那么您将无法使用索引.

您可以做的是将您的努力结合如下:

{
    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'}
             }
          }
    ]
 }
Run Code Online (Sandbox Code Playgroud)

以上只为9月汇总,如果你想聚合多个月,你可以举例如:

{
    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'}
             }
          }
    ]
 }
Run Code Online (Sandbox Code Playgroud)

你会得到类似的东西:

{
    "result" : [
        {
            "_id" : {
                "yr" : 2012,
                "mo" : 7
            },
            "totalComments" : 5
        },
        {
            "_id" : {
                "yr" : 2012,
                "mo" : 8
            },
            "totalComments" : 19
        },
        {
            "_id" : {
                "yr" : 2012,
                "mo" : 9
            },
            "totalComments" : 21
        }
    ],
    "ok" : 1
}
Run Code Online (Sandbox Code Playgroud)