KeplerGrid guide

Group MongoDB orders by day with $dateTrunc

Use $dateTrunc to assign each order to a calendar day, then use $group to count orders and sum their totals. Pick the reporting time zone to decide which side of midnight each order lands on.

Create four synthetic orders

Run this in mongosh against a disposable development server with MongoDB 5.0 or later. Use an empty practice database named keplergrid_daily_orders. The order times are BSON Dates and the amounts are integer cents.

const practice = db.getSiblingDB("keplergrid_daily_orders");
practice.orders.insertMany([
  { _id: "O-101", customerId: "C-100",
    placedAt: new Date("2026-06-10T23:30:00Z"), totalCents: 1200 },
  { _id: "O-102", customerId: "C-100",
    placedAt: new Date("2026-06-11T00:30:00Z"), totalCents: 800 },
  { _id: "O-103", customerId: "C-100",
    placedAt: new Date("2026-06-11T00:30:00Z"), totalCents: 700 },
  { _id: "O-201", customerId: "C-200",
    placedAt: new Date("2026-06-11T04:15:00Z"), totalCents: 900 }
]);

Count and sum orders for each UTC day

Connect KeplerGrid to the practice server using a Read only connection. Open keplergrid_daily_orders.orders, select Aggregation → JSON, paste this pipeline, and choose Run pipeline.

[
  { "$group": {
    "_id": { "$dateTrunc": {
      "date": "$placedAt",
      "unit": "day",
      "timezone": "UTC"
    } },
    "orderCount": { "$sum": 1 },
    "totalCents": { "$sum": "$totalCents" }
  } },
  { "$sort": { "_id": 1 } }
]

Each row represents one day. Its _id is the instant that day starts. orderCount adds one per order and totalCents adds the stored amounts. Switch to Stages and select $group to compare the four orders in Input with the two buckets in Output.

Change the reporting time zone

Return to JSON, change "timezone": "UTC" to "timezone": "America/New_York", and run the pipeline again. In this June example, midnight in New York is 04:00 UTC. The two orders at 00:30 UTC on June 11 therefore contribute to the June 10 bucket in New York.

Expected buckets and totals for the same four orders
Reporting dayBucket start in UTCOrdersTotal cents
June 10, UTCJun 10, 00:0011200
June 11, UTCJun 11, 00:0032400
June 10, New YorkJun 10, 04:0032700
June 11, New YorkJun 11, 04:001900

Use the named time zone for reports that span daylight saving changes. If you later add a date-range filter before $group, convert both reporting boundaries into UTC instants so the filter and buckets use the same calendar.

Use the same fixture to find the latest order per customer. You can also export your daily totals to CSV or Extended JSON.

To compare two timestamps on each document, measure the difference with $dateDiff.

To keep individual orders and add their cumulative values, calculate a running total per customer.

Next, Fill missing dates in MongoDB with $densify.