SQL and data types · Engineering
Dates, time zones, and timestamp comparisons
How NonoDB accepts timestamps, normalizes them to UTC, compares instants, and avoids the ambiguous-local-time trap.
Dates look simple until a client sends a local time, a daylight-saving boundary, or a string that looks like a date but is not one. NonoDB treats dates as BSON-compatible instants and accepts several input forms, but it is strict about ambiguity.
The short rule: a stored date is a UTC instant. Everything else is an input or presentation concern.
Accepted input forms
Relaxed Extended JSON with an offset:
{"created_at": {"$date": "2026-01-01T01:00:00+01:00"}}Canonical Extended JSON with epoch milliseconds:
{"created_at": {"$date": {"$numberLong": "1767225600000"}}}Numeric convenience form, also interpreted as Unix milliseconds:
{"created_at": {"$date": 1767225600000}}All accepted forms are normalized to canonical UTC milliseconds before storage.
What is rejected
Naive local times are rejected with invalid_date:
{"created_at": {"$date": "2026-01-01T00:00:00"}}The instant is ambiguous without an offset. Date-only strings are also rejected:
{"created_at": {"$date": "2026-01-01"}}Store an explicit instant such as 2026-01-01T00:00:00Z if that is what you mean. Rejecting ambiguity is safer than guessing the server’s local zone.
Comparison is by instant
These three values are equal:
2026-01-01T00:00:00Z2026-01-01T01:00:00+01:002026-01-01T02:00:00+02:00$eq, $ne, $in, and $nin compare dates by instant. Range filters use the normal comparison operators:
{ "op": "find", "coll": "events", "filter": { "created_at": { "$gte": {"$date": "2026-01-01T00:00:00Z"}, "$lt": {"$date": "2026-02-01T00:00:00Z"} } }}There are no separate $dateGt operators. Dates participate in the normal filter language.
Dates and strings are different types
A date query does not match an ISO string field, and a string query does not match a date field.
{"created_at": "2026-01-01T00:00:00Z"}That document stores a string. It will not be found by a $date range. This is a common migration mistake: an exported JSON file may contain date-looking strings that were never BSON dates. Validate the field types before cutover.
Indexes and sorting
Secondary indexes encode dates as order-preserving signed integers. A normal index is enough; no type annotation is required.
{"op": "create_index", "coll": "events", "fields": ["tenant", "created_at"]}This supports range filters after an equality on tenant, chronological sorting, unique indexes that treat different offsets for the same instant as duplicates, covering projections with date fields, and dates before 1970.
The index format matters. Older indexes that predate date support are skipped for date filters; queries fall back to a scan until the index is recreated. Check the index format after an upgrade.
Time zones in expressions
Timezone identifiers such as Europe/Warsaw are not part of the stored value. The stored value is always the UTC instant.
Date expression operators can take an IANA zone to interpret local civil dates. The result is still stored as UTC. Supported operations include $dateAdd, $dateSubtract, $dateDiff, $dateTrunc, $dateToString, $dateFromString, $dateFromParts, and $dateToParts, with units from year to millisecond and optional startOfWeek.
This is how MongoDB’s BSON Date works too: the instant is universal, and the zone is a presentation or computation parameter.
Updates
Dates can be set with normal operators:
{ "op": "update", "coll": "events", "filter": {"_id": "e1"}, "update": {"$set": {"created_at": {"$date": "2026-01-01T00:00:00Z"}}}}$currentDate sets the current UTC date. BSON Timestamp values are also supported for fields that need the timestamp type:
{"update": {"$currentDate": {"ts": {"$type": "timestamp"}}}}TTL and time-series
Date fields can drive TTL. The document expires at the date plus the configured seconds; expired documents are hidden immediately by default. Missing or non-date fields never expire.
Time-series collections have their own time field rules: a missing or non-date value is rejected, and retention is relative to the event time. Changing the time or meta field moves the row to a different bucket.
A migration checklist
- Convert date-looking strings to
$datebefore import. - Keep time zones as offsets in input, not as stored strings.
- Recreate indexes that predate date support.
- Test range filters across DST boundaries.
- Use
explainto confirm the date index is used. - Check TTL behavior with a real date field.
The dates guide covers input validation, date expressions, migration, and index compatibility in more detail.