Load the practice collection
Run once in mongosh against a development instance with an empty reports practice collection.
const lab = db.getSiblingDB("keplergrid_practice")
lab.reports.insertMany([
{ _id: "north", totals: { shipped: 12, returned: 2 } },
{ _id: "south", totals: { shipped: 8, pending: 3 } },
{ _id: "new" }
])
In KeplerGrid, open keplergrid_practice.reports with a Read only connection.
Convert the object and unwind the entries
In Aggregation → JSON, run the pipeline. $objectToArray gives each top-level field an entry with its key k and value v.
[
{ "$set": { "entries": { "$objectToArray": { "$ifNull": ["$totals", {}] } } } },
{ "$unwind": "$entries" },
{ "$project": { "_id": 0, "region": "$_id", "metric": "$entries.k", "count": "$entries.v" } },
{ "$sort": { "region": 1, "metric": 1 } }
]
The result has four rows. North has returned 2 and shipped 12. South has pending 3 and shipped 8. The region with no metrics has no rows because the empty object yields an empty array.
Check the generated key and value pairs
Open Stages and inspect $set to see the object become a list of entries. Nested documents remain as values in those entries. If you need to keep regions with no metrics, use preserveNullAndEmptyArrays in the unwind stage and decide how to display the empty row.
See how to choose how to handle empty arrays when unwinding or revisit the related KeplerGrid walkthrough.