KeplerGrid guide

Combine MongoDB documents with $mergeObjects

Apply optional settings over defaults in an aggregation. Check which keys win and how explicit null and nested objects behave.

Load the practice collection

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

const lab = db.getSiblingDB("keplergrid_practice")
lab.projects.insertMany([
  { _id: "a", overrides: { alerts: false, format: null, display: { mode: "compact" } } },
  { _id: "b", overrides: null },
  { _id: "c", overrides: { format: "csv" } }
])

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

Apply the defaults first and the overrides second

Run this pipeline in Aggregation → JSON. The first operand is the defaults document. The second operand supplies any keys that override it.

[
  { "$set": { "settings": { "$mergeObjects": [
    { "alerts": true, "format": "json", "display": { "mode": "full", "density": "normal" } },
    "$overrides"
  ] } } },
  { "$sort": { "_id": 1 } }
]

Project A changes alerts to false and explicitly clears format to null. The null operand on project B is ignored, so B keeps every default. C changes only format to CSV.

Check what happens to nested keys

Project A’s display object replaces the entire previous value. It loses the default density key. Combine nested objects separately if your application needs to merge their individual keys.

See how to normalize fields before combining objects or revisit the related KeplerGrid walkthrough.