What is the purpose of the $inc operator in MongoDB?

QuestionsQuestions8 SkillsProUpdate MongoDB RecordsOct, 13 2025
0194

The $inc operator in MongoDB is used to increment the value of a field by a specified amount. It is commonly used to update numeric fields in documents. If the field does not exist, $inc will create it and set it to the specified value.

Example:

db.collection.updateOne(
   { _id: 1 }, // Filter to find the document
   { $inc: { quantity: 5 } } // Increment the 'quantity' field by 5
)

In this example, the quantity field of the document with _id 1 will be increased by 5. If quantity does not exist, it will be created with a value of 5.

0 Comments

no data
Be the first to share your comment!