MongoDB Aggregate Framework - Group by Year

后端 未结 2 540
借酒劲吻你
借酒劲吻你 2020-12-19 04:54

I\'ve been trying to use the aggregate function to group date fields by year:

db.identities.aggregate([
{
    $group : {
        _id : { year : {$year : \"$b         


        
相关标签:
2条回答
  • 2020-12-19 05:42

    Some versions of Windows have been known to work. By any chance, are you using a 32-bit OS? The code in question is here, and depends upon the gmtime_s() implementation.

    If this collection is simply for aggregation queries, you can certainly get by with storing date components in an object. I'd suggest abbreviating the field names (e.g. y, m, d) to save on storage, since the field strings are present in each stored document. The trade-off here is that none of the aggregation date operators can be used. You may want to store the timestamp as a signed integer (e.g. ts) so that you can easily do range queries if necessary.

    0 讨论(0)
  • 2020-12-19 05:50

    This issue is caused by the minimum date not being handled well when aggregated.

    A quick workaround (if possible) is to filter the date, either through the ff:

    • Remove the rows with minimum date (via match / filter)
    • Filter through a certain date (greater than x date)

    Then aggregate.

    0 讨论(0)
提交回复
热议问题