$inc

noun

say it: “DOL-er INK” /ˈdɑlər ˈɪŋk/

A MongoDB update operator that adds an amount to a number field, or subtracts with a negative amount.

What does $inc do in a MongoDB update?

$inc adds a number to a field in the database itself, so two updates at the same moment both count. A negative number subtracts, and a field that isn't there yet starts from 0.

db.recipes.updateOne({ _id: 4 }, { $inc: { saves: 1 } });
db.recipes.findOne({ _id: 4 }).saves;
// 13

In a sentence

  • “Selling a bike runs $inc with -1 on its stock.”
  • “Each tap of the heart runs $inc on the recipe's saves, so no tap is lost.”

Level: Apprentice · More MongoDB terms at this level

Updated