Open In App

MongoDB – $inc Operator

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB $inc or increment operator is a type of field update operator. $inc operator increases the values of the fields to the specified amount or increases the field by the given value.

Important Points

  • This operator accepts positive and negative values.
  • If the given field does not exist, then this operator will create a field and set the value of that field.
  • This operator will generate an error, if you use this operator with a null value field.
  • It is an atomic operation in a single document.

Syntax

{ $inc: { field1: amount1, field2: amount2, ... } }

MongoDB $inc Operator Example

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: contributor

Document: three documents that contain the details of the contributors in the form of field-value pairs

demo database and collection creation

Example 1: Increment the value of the field using $inc operator

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:

example 1 output

Example 2: Increment the value of the field in the array using $inc operator

In this example, we are updating the field of an employee’s document whose name is Mohit by incrementing the value of a field to 10.

Query:

db.contributor.update({name: "Priya", "points._id": "g_1"}, {$inc: {"points.$.a":10}})

Output:

example 2 output

Example 3: Increment the value of the field in embedded document using $inc operator

In this example, we are updating the field of an employee’s document whose name is Mohit by incrementing the value of a rank to 2.

Query:

db.contributor.update({name: "Amu"}, {$inc: {"personal.rank": 2}})

Output:

example 3 output

Key Takeaways About $inc Operator

  • The $inc operator in MongoDB increments the value of a field by a specified amount.
  • It can be used in embedded documents or arrays using dot notation.
  • The $inc operator can be used in methods like update(), updateOne() according to requirements.
  • Use of the $inc operator on a field with a null value will generate an error.
  • It is an atomic operation within a single document.

Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database used by developers worldwide for its flexibility, scalability and ease of use. Two key components of the MongoDB ecosystem are MongoDB Compass and MongoDB Atlas. While both are essential tools for working with MongoDB databases, they serve different purposes and cater to different user needs. In this article, We
4 min read
MongoDB - Equality Operator $eq
MongoDB provides different types of comparison operators and an equality operator($eq) is one of them. The equality operator( $eq ) is used to match the documents where the value of the field is equal to the specified value. In other words, the $eq operator is used to specify the equality condition. Important Points: If the given value is a documen
2 min read
MongoDB - Greater than Operator $gt
MongoDB provides different types of comparison operators and greater than operator($gt) is one of them. This operator is used to select those documents where the value of the field is greater than(>) the given value. You can use this operator in methods (like, find, update, etc.) according to your requirements. Syntax: {field: {$gt: value}} In t
2 min read
MongoDB - Greater than equals to Operator $gte
MongoDB provides different types of comparison operators and greater than equals to operator($gte) is one of them. This operator is used to select those documents where the value of the field is greater than equals to(>=) the given value. You can use this operator in methods (like, find(), update(), etc.) according to your requirements. Syntax:
2 min read
MongoDB AND operator ( $and )
MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. ac
2 min read
MongoDB NOT operator ( $not )
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression. It also includes those documents that don't contain the field. You can
2 min read
MongoDB $in Operator
MongoDB provides different types of comparison query operators and $in operator is one of them. This operator is used to select those documents where the value of the field is equal to any of the given value in the array. And if the field contains an array, then this operator selects only those documents whose field contains an array that holds at
2 min read
MongoDB OR operator ( $or )
MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array. You can use this operator in methods like find(), update(
2 min read
Article Tags :