KeplerGrid guide

Query a MongoDB date range with $gte and $lt

Use two BSON Date boundaries to include the start of a reporting period and exclude the beginning of the next one.

Load the practice collection

Run once in mongosh against a development instance with an empty events practice collection.

const lab = db.getSiblingDB("keplergrid_practice")
lab.events.insertMany([
  { _id: "previous", occurredAt: ISODate("2026-03-31T23:59:59.999Z") },
  { _id: "start", occurredAt: ISODate("2026-04-01T00:00:00.000Z") },
  { _id: "last", occurredAt: ISODate("2026-04-01T23:59:59.999Z") },
  { _id: "next", occurredAt: ISODate("2026-04-02T00:00:00.000Z") },
  { _id: "legacy", occurredAt: "2026-04-01T12:00:00.000Z" }
])

In KeplerGrid, open keplergrid_practice.events with a Read only connection.

Run the date range in Documents

Paste this into the Documents filter. The $date wrappers give both bounds the BSON Date type.

{
  "occurredAt": {
    "$gte": { "$date": "2026-04-01T00:00:00.000Z" },
    "$lt": { "$date": "2026-04-02T00:00:00.000Z" }
  }
}

Expect start and last. The timestamp string on legacy has a different BSON type and misses the date range. Reuse the upper bound as the lower bound of the next query to avoid double-counting documents at midnight.

Set the reporting time zone first

For a local calendar day, convert the start of that day and the start of the following day to UTC separately. The interval can be 23 or 25 hours across a daylight saving change.

See how to audit the types stored in a field or revisit the related KeplerGrid walkthrough.

Next, Format MongoDB dates by time zone with $dateToString.