I want to find the max and min values of the myUpdateTime
field. It’s a timestamp but stored as a string.
"myUpdateTime": "2021-11-08T17:42:30.650Z",
I want to find the max and min values of the myUpdateTime
field. It’s a timestamp but stored as a string.
"myUpdateTime": "2021-11-08T17:42:30.650Z",
2
Answers
To find the maximum and minimum values of the "myUpdateTime" field, you can convert the timestamp strings to actual Date objects and then compare them.
Would recommend that you should store the Timestamp value as
Timestamp
rather thanstring
to improve the performance in case that every time you need to convert the field into Timestamp such for these scenarios:To get the min/max of
myUpdateTime
, you have to use$group
operator to group by all documents. And perform themin
andmax
value by converting themyUpdateTime
intoTimestamp
value first.Demo @ Mongo Playground