KeplerGrid guide

Find distinct MongoDB values and count their documents

List each value once and then add counts for a report. Compare mongosh distinct with an aggregation you can inspect in KeplerGrid.

Load the practice collection

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

const lab = db.getSiblingDB("keplergrid_practice")
lab.dispatches.insertMany([
  { _id: 1, warehouse: "north", state: "shipped" },
  { _id: 2, warehouse: "north", state: "shipped" },
  { _id: 3, warehouse: "north", state: "queued" },
  { _id: 4, warehouse: "south", state: "cancelled" }
])

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

List each state once

Use distinct in the same shell session when you need just the labels. The second argument limits the lookup to the north warehouse.

lab.dispatches.distinct("state", { warehouse: "north" })

Expect queued and shipped, without relying on their order. This practice field uses scalar strings. MongoDB distinct treats elements of a stored array as separate values.

Return one row per state with its count

Paste this pipeline into KeplerGrid Aggregation → JSON. The match limits the documents before grouping, and the final sort orders equally frequent values alphabetically.

[
  { "$match": { "warehouse": "north" } },
  { "$group": { "_id": "$state", "documents": { "$sum": 1 } } },
  { "$sort": { "documents": -1, "_id": 1 } }
]

The result contains shipped with two documents and queued with one. For array-valued source fields, grouping directly uses the entire array as the group key. Unwind first when you need to count individual elements.

See how to return multiple summaries in one aggregation or revisit the related KeplerGrid walkthrough.