If you tried querying by dates in MongoDB, you know how much of a pain it can be. Even if you know the operators, typing the query will raise doubts. Is it ISODate, isoDate or IsoDate? What exactly is the date format? What was the date 2 weeks ago?
Mingo uses human-readable tags for dates in queries. Compare the following two queries:
Regular MongoDB date format:
{
"createAt": {
"$gte": ISODate("2020-02-21T00:00:00Z"), // or new Date(…)
"$lt": ISODate("2020-02-28T00:00:00Z") // or new Date(…)
}
}
Using Mingo’s date shorthand:
{
"createdAt": #lastWeek
}
There are two main types of date shorthands available:
Mingo provides the following duration tags:
#today | since the start of the today |
#yesterday | during yesterday |
#tomorrow | during tomorrow |
#thisWeek | during the current week |
#thisMonth | during the current month |
#thisYear | during the current year |
#lastWeek | during the previous week |
#lastMonth | during the previous month |
#lastYear | during the previous year |
#last{number}{period} | During the last {number} {period}. Use any number and one of the following periods: minutes, hours, days, weeks, months, years. This is case insesitive, so you may use #last4Years. Examples: #last2days, #last14weeks, #last3years |
#next{number}{period} | During the next {number} {period}. Use any number and one of the following periods: minutes, hours, days, weeks, months, years. This is case insesitive, so you may use #next4Years. Examples: #next2days, #next14weeks, #next3years |
#2021-11-22 | during any specific date |
#2021-11 | during any specific month |
#2021 | during any specific year |
#2021-11-22:2021-11-25 | between two dates, inclusive |
#2021-11:2021-12 | between two months, inclusive |
#2021:2022 | between two years, inclusive |
#since2017-10-03 | since any specific date |
#before2017-10-03 | before any specific date |
#until2017-10-03 | until any specific date, including the date |
#after2017-10-03 | after any specific date |
To query by a specific moment, use specific dates with the @ prefix, such as @2021-01-29
or @2020-01-29T17:29
.
@now or #now | matches the moment the query was submitted |
@2020-01-29T17:29:50+00:00 | ISO date format - matches specific moment |
@17:29:50 | matches specific hour:minute:second |
@17:29 | matches specific hour:minute |
Mingo will understand any valid ISO 8601 format: https://en.wikipedia.org/wiki/ISO_8601
#2021-02-25
and @2021-02-25
are not the same. First refers to a duration (during that day from midnight to midnight) while the second points to the beginning of that day (a specific moment).