KeplerGrid guide

Insert or update a MongoDB document with upsert

Use a stable ID, $set for current fields, and $setOnInsert for creation defaults that should survive later updates.

Load the practice collection

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

const lab = db.getSiblingDB("keplergrid_practice")
lab.syncItems.insertOne({ _id: "seed", status: "practice fixture" })

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

Insert the first version with a stable ID

Run this command in mongosh. Starting with an unused ID exercises the insert branch. $setOnInsert applies only when this update creates a document.

lab.syncItems.updateOne(
  { _id: "source-401" },
  {
    $set: { status: "queued" },
    $setOnInsert: { createdAt: ISODate("2026-04-01T08:00:00Z") }
  },
  { upsert: true }
)

The command matches zero existing documents. In KeplerGrid Documents, filter for source-401 and confirm the new row has both fields.

Run a second sync for the same ID

Update the status and intentionally supply a different creation date in the insert-only branch.

lab.syncItems.updateOne(
  { _id: "source-401" },
  {
    $set: { status: "complete" },
    $setOnInsert: { createdAt: ISODate("2026-04-02T08:00:00Z") }
  },
  { upsert: true }
)

The existing row changes to complete. The date remains April 1 at 08:00 UTC, and the command reports one match and one modification. When using a different natural key, enforce the intended uniqueness with an index, especially if two jobs can upsert at the same time.

See how to enforce uniqueness for a key with two fields or revisit the related KeplerGrid walkthrough.