MongoDB - $inc Operator
The MongoDB $inc operator is one of the most commonly used update operators in MongoDB, especially when it comes to modifying numerical values within documents. It is used to increment or decrement the value of a field by a specific amount, making it highly useful for applications like counters, scores, and inventory systems.
In this article, we will explore the $inc operator in MongoDB, its syntax, use cases, and provide step-by-step examples to help you understand its practical implementation. Whether you're building real-time systems or managing counters in your application, the $inc operator is a critical tool to master.
What is MongoDB $inc Operator?
The MongoDB $inc operator is used to update the value of a field by a specified increment or decrement. This operation can be applied to numeric fields, and it can handle both positive and negative values. Unlike traditional SQL operations, MongoDB's $inc operation is atomic within a document, ensuring that no other operations can interfere with the update while it’s in progress.
Key Features
- The $inc operator can either increment or decrement values by any numeric value, including both positive and negative numbers.
- If the field does not exist, MongoDB creates the field and sets the value to the increment value.
- The $inc operator is an atomic operation, meaning it ensures data consistency when updating a document in a single operation.
- Using $inc on a null value field will result in an error, so you should always ensure that the field being updated contains numeric values.
Syntax
{ $inc: { field1: amount1, field2: amount2, ... } }
MongoDB $inc Operator Example
In this section, we will explore the usage of the MongoDB $inc operator with practical examples. To demonstrate how this works, we'll use a sample database called GeeksforGeeks, which contains a contributor collection. Each document within this collection holds information about different contributors, including their name, number of articles published, points earned, and personal details such as rank.
- Database: GeeksforGeeks
- Collection: contributor
- Document: three documents that contain the details of the contributors in the form of field-value pairs

Example 1: Incrementing a Field Value
In this example, we are updating the fields of an employee's document whose name is Mohit by incrementing the value of publish articles field to 10 and decreasing the value of the salary field to -100.
Query:
db.contributor.update(
{ name: "Mohit" },
{ $inc: { publisharticles: 10, salary: -100 } }
);
Output:

Explanation:
- The $inc operator increases the value of
publisharticlesby10and decreases thesalaryfield by100within the same document. - This operation is atomic, meaning no other process can alter the document during the update.
Example 2: Incrementing a Field in an Array Element
In this example, we are going to update the value of a nested field a in the arraypoints, for a specific document where the name is "Priya" and the points._id is "g_1"
Query:
db.contributor.update({name: "Priya", "points._id": "g_1"}, {$inc: {"points.$.a":10}})Output:

Explanation:
- The use of dot notation allows us to access and modify the field within an array.
- The
points.$.arefers to the first element in the array where_idmatches"g_1". - The $inc operator applies only to that specific array element, making this operation more targeted and efficient.
Example 3: Incrementing a Field in an Embedded Document
In this example, we will increment the value of the rank field in an embedded documentpersonal for a document where the name is "Amu".
Query:
db.contributor.update({name: "Amu"}, {$inc: {"personal.rank": 2}})Output:

Explanation:
- Dot notation (
personal.rank) is used to target therankfield inside the embeddedpersonaldocument. - The
$incoperator performs the increment operation atomically, which ensures data consistency when modifying nested fields.
Important Points for using $inc Operator
- Field Existence: If the field to be updated doesn’t exist, MongoDB will create the field and assign it the increment value. This behavior is particularly useful when working with documents that may not have all fields defined upfront.
- Null Fields: Applying the
$incoperator to fields with a null value will result in an error. Always ensure that the fields contain numeric values before using$inc. - Atomic Operations: The
$incoperator ensures that the operation is atomic for a single document. This prevents issues related to concurrency and data inconsistency, especially in high-traffic applications.
Conclusion
The MongoDB $inc operator is a powerful tool for performing efficient updates to numeric fields within documents. By incrementing or decrementing values, we can easily manage counters, scores, and other numerical data. Whether you're updating simple fields or modifying values within embedded documents and arrays, the $inc operator provides an easy and atomic solution for handling numeric updates. By understanding and effectively using the$inc operator, developers can streamline database operations and ensure data integrity across MongoDB-based applications.