Load the practice collection
Run once in mongosh against a development instance with an empty stock practice collection.
const lab = db.getSiblingDB("keplergrid_practice")
lab.stock.insertOne({ _id: "PAPER-01", remaining: 5 })
In KeplerGrid, open keplergrid_practice.stock with a Read only connection.
Reserve two units with one conditional update
Run this command in the same mongosh session. The filter checks both the product ID and whether at least two units remain. MongoDB applies the decrement to the matching document atomically.
lab.stock.updateOne(
{ _id: "PAPER-01", remaining: { $gte: 2 } },
{ $inc: { remaining: -2 } }
)
The update reports one match and one modification. In KeplerGrid Documents, filter by the product ID to see remaining: 3. Use the shell for the update and KeplerGrid to inspect the document.
Check what happens when there is insufficient stock
Try reserving four units with the same filter structure. Keep the threshold and decrement in sync.
lab.stock.updateOne(
{ _id: "PAPER-01", remaining: { $gte: 4 } },
{ $inc: { remaining: -4 } }
)
The command matches zero documents and the count stays at three. The guard assumes a numeric count. Repeating a successful decrement intentionally applies it again, so verify an uncertain result before manually rerunning it.
See how to combine conditions in a MongoDB filter or revisit the related KeplerGrid walkthrough.