One of the time fields stored in mongodb is the string type. how to filter the data by time?

mongodb database has a day field that stores the string type, as shown in figure

.

now I"m going to filter the data for different time periods through the day field, but write

like this
 req.collections.dayTrainings.count({ "phone": req.param("phone"), "day": { $lte: theday } }, function (error, trainingCount) {})

the data found is not correct because it is a string type. What should I do to correctly find out the data for a period of time

Mar.21,2021

emphasize that do not use strings to save time . If you have already saved it, change it to ISODate, as soon as possible because there will be other problems sooner or later.
first of all, a normal epoch time is 32 bits (4 bytes), a string date is at least 10 bytes (e.g. 2018-06-12 ), and the string time is at least 19 bytes (e.g. 2018-06-12 12:00:00 ).
second, the time of the string cannot be operated normally. In normal time, you can easily take out the corresponding part of the string through the operator such as $year/$month/$day, and the time of the string.
again, what should I do if I encounter a time zone problem?

finally, let's talk about your problem. I don't know what theday is, so I can't verify whether my inference is correct.
consider which of the following two times is larger?

  • timeA = '2018-6-12'
  • timeB = '2018-12-12'
In terms of common sense, we must feel timeB > timeA , but for computers, timeA > timeB . This is also one of the many problems with using string storage time. The machine comparison time is compared byte by byte. The first five of you are all '2018muri' , so it's a tie. The sixth place decides the outcome, one is 6, the other is 1, and the result is, of course, timeA timeB .
so if the above reasons are not enough to convince you to use ISODate, you should at least make up for the remaining 0 strong 2018- 0 6-12'.

Menu