MongoDB Unified Structure - Group by Year

I am trying to use an aggregate function to group date fields by year:

db.identities.aggregate([ { $group : { _id : { year : {$year : "$birth_date"}}, total : {$sum : 1} } } ]) 

Some of my dates, however, fall before 1970 and are Windows users, I get an unpleasant error in gmtime:

 { "errmsg" : "exception: gmtime failed - your system doesn't support dates before 1970", "code" : 16422, "ok" : 0 } 

I know that the obvious answer is now for me to start a virtual machine or something like that, but I was just curious if there were any problems for Windows (Windows 7 in my case). If you cannot save maximum performance as a nested object, then ie:

 birth_date : { year : 1980, month : 12, day : 9 } 

I'm not too sure how much this would be with indexes, etc.

Any advice appreciated!

+8
javascript mongodb
source share
1 answer

It is known that some versions of Windows work. Do you accidentally use a 32-bit OS? The code in question here depends on the implementation of gmtime_s() .

If this collection is just for aggregation requests, you can certainly get by with storing date components in an object. I would suggest abbreviation of field names (for example, y , m , d ) for storage, since field strings are present in every saved document. The trade-off here is that none of the aggregation date operators can be used. You may want to save the timestamp as a signed integer (for example, ts ) so that you can easily execute range queries if necessary.

0
source share

All Articles