0

I currently am trying to remove milliseconds from a datetime object in my aggregation of a pymongo query.

I'm grouping by one of their IDs, which they all have in common. I also need the date though.

The current date won't let me group correctly because some objects are 1 millisecond different, so it groups with most of the objects in the grouping, but maybe one of the objects gets left out because of the millisecond time difference.

Edit: I've converted it to a string and it seems to have worked. How do I convert it to a date again within the aggregation. $dateFromString is not working

Is there a way that I can remove the milliseconds so the correct dates all correspond to the same ID?

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Take a look at: https://stackoverflow.com/questions/5476065/how-to-truncate-the-time-on-a-datetime-object-in-python – fsl Jul 18 '18 at 00:46
  • You can use `db.colname.aggregate({ "$group":{ "_id":{"$subtract":["$date",{"$millisecond":"$date"}]} } });` to drop millis while still keeping as date type. – s7vr Jul 18 '18 at 01:37

0 Answers0