skipped count 0 in aggregate function

后端 未结 3 1188
太阳男子
太阳男子 2020-12-20 01:25

I\'m stuck on this for couple of days. I\'m trying to get the count: 0 where there is no documents in the given time period. This is the aggregate function I\'m

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 01:46

    The group stage is producing documents based on grouping on your given _id and counting the number of documents from the previous stage that end up in the group. Hence, a count of zero would be the result of a document being created from 0 input documents belonging to the group. Thinking about it this way, it's clear that there's no way the aggregation pipeline can do this for you. It doesn't know what all of the "missing" time periods are and it can't invent the appropriate documents out of thin air. Reapplying your extra knowledge about the missing time periods to complete the picture at the end seems like a reasonable solution (not "hacky") if you need to have an explicit count of 0 for empty time periods.

提交回复
热议问题