MongoDB Aggregate Framework - Group by Year

后端 未结 2 539
借酒劲吻你
借酒劲吻你 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.

提交回复
热议问题