Open In App

MongoDB – $inc Operator

Last Updated : 23 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Next Article

Similar Reads

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 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
MongoDB NOR operator ( $nor )
MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(),
2 min read
MongoDB - $ne Operator
MongoDB $ne or "not equals" operator is one of the comparison operators. The $ne operator selects those documents where the field value is not equal to the given value. It also includes those documents that do not contain the specified field. You can use this operator in methods like find(), update(), etc. as per your requirement. Syntax{field: {$n
2 min read
MongoDB - $nin Operator
MongoDB $nin or " not in" is one of the comparison query operators. The $nin operator selects those documents where the field value is not equal to any of the given values in the array and the field that does not exist. You can use this operator in methods like find(), update(), etc. according to your requirements. Syntax{field: {$nin: [value1, val
2 min read
MongoDB - $max Operator
MongoDB $max or maximum operator is one of the field update operators. $max operator updates the field with the specified value if the specified value is greater than the current value. Important Points This operator will compare the values of different data types according to the BSON comparison order.You can also use this operator in embedded/nes
3 min read
MongoDB - $min operator
MongoDB $min or minimum operator is one of the field update operators. $min operator updates the field with the specified value if the specified value is less than the current value. The $min operator will compare the values of different data types according to the BSON comparison order. This operator can also be used in embedded/nested documents u
3 min read
MongoDB - $lt Operator
MongoDB $lt operator or less than operator is one of the comparison operators. The $lt operator selects the documents with a value less than (<) the given value. You can use this operator in methods like, find(), update(), etc. as per your requirements. Syntax{field: {$lt: value}}MongoDB $lt Operator ExampleIn the following examples, we are work
2 min read
MongoDB - $lte Operator
MongoDB $lte Operator is one of the comparison operators. $lte operator selects those documents where the field value is less than equal to (<=) the given value. This operator can be used in methods like find(), update(), etc. according to your requirements.. Syntax{field: {$lte: value}}MongoDB $lte Operator ExampleIn the following examples, we
2 min read
MongoDB $gt Operator
MongoDB $gt operator selects documents where the value of a field is greater than a specified value. $gt Operator in MongoDBThe $gt operator in MongoDB is a comparison operator. It is used to find documents where the value in the field is greater than(>) a given value. This operator can be used with other methods like find, update, etc. accordin
2 min read
MongoDB - $gte Operator
MongoDB $gte or "greater than equals to" operator is one of the comparison operators. $gte operator selects those documents where the field value is greater than equals to(>=) the given value. This operator can be used in methods (like, find(), update(), etc.) according to your requirements. Syntax{field: {$gte: value}}MongoDB $gte Operator Exam
2 min read
MongoDB - $eq Operator
MongoDB $eq operator or equality operator is a comparison operator. The $eq operator matches documents where the value of the field is equal to the specified value. Important Points: If the given value is a document, then the order of the fields in the document is important.If the given value is an array, then MongoDB matches the documents where th
2 min read
MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 min read
MongoDB - $setOnInsert Operator
The $setOnInsert operator in MongoDB is a powerful tool used in updating operations with the upsert option. It allows us to specify values that should be set only when a new document is inserted. In this article, we will learn about the $setOnInsert Operator in MongoDB in detail and so on. MongoDB $setOnInsert OperatorMongoDB $setOnInsert Operator
4 min read
MongoDB - Positional Operator ($)
MongoDB provides a variety of array update operators to modify the values within array fields in documents. One such operator is the positional operator $. This operator allows us to update an element in an array without explicitly specifying the position of that element. In this article, We will learn about the Positional Operator($) in MongoDB in
4 min read
MongoDB - $pullAll Operator
MongoDB $pullAll operator is a crucial tool for efficiently managing arrays within MongoDB documents. It allows users to remove all instances of specified values from an array and provides a direct way to update array fields. In this article, We will learn about the MongoDB $pullAll Operator by understanding various examples in detail. MongoDB $pul
3 min read
MongoDB - $mul Operator
MongoDB $mul operator is a powerful update operator used to multiply the value of a field by a specified number. This operator allows for direct arithmetic operations within the database, making it particularly useful for scenarios that require modifying numeric field values without needing to retrieve the current value first. In this article, We w
4 min read
MongoDB - $pop Operator
The $pop operator in MongoDB is designed for managing array fields by removing either the first or last element of the array. This operator simplifies the process of maintaining array size and managing data in scenarios where a fixed number of elements is required. In this article, We will learn about the MongoDB $pop Operator in detail by understa
4 min read
MongoDB $pull Operator
The $pull operator in MongoDB is a powerful update operator used to remove all instances of a specified value or values from an array. This operator is particularly useful for modifying arrays within documents without retrieving and updating the entire array manually. In this article, We will learn about the MongoDB $pull Operator by understanding
5 min read
MongoDB Query and Projection Operator
MongoDB query operators are essential tools for interacting with data stored in MongoDB collections. Similar to SQL, MongoDB offers a range of operators to perform various operations such as comparisons, logical evaluations, and array manipulations. These operators allow users to efficiently query, filter, and manipulate data based on specific cond
11 min read
MongoDB $in Operator
MongoDB $in operator provides a powerful way to query documents based on multiple potential values for a specific field within a single query. In this article, We will learn about the MongoDB $in Operator by understanding various examples in detail. MongoDB $in OperatorThe MongoDB $in operator is used to match documents where the value of a field e
4 min read
MongoDB - Current Date Operator ($currentDate)
MongoDB provides different types of field update operators to update the values of the fields of the documents and $currentDate operator is one of them. This operator is used to set the value of a field to the current date (either as a timestamp or as a Date). The default type of $currentDate operator is a Date.This operator can also work with embe
2 min read
MongoDB $push Operator
The $push operator in MongoDB is a powerful tool used to update arrays within documents. It appends the entire array as a single element. To add each element of the array individually, the $each modifier can be used alongside $push. In this article, We will learn about the MongoDB $push Operator by understanding various examples in detail. $push Op
5 min read
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 known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
MATLAB - Image Edge Detection using Prewitt Operator from Scratch
Prewitt Operator: It is a gradient-based operator. It is one of the best ways to detect the orientation and magnitude of an image. It computes the gradient approximation of image intensity function for image edge detection. At the pixels of an image, the Prewitt operator produces either the normal to a vector or the corresponding gradient vector. I
3 min read
MATLAB - Image Edge Detection using Sobel Operator from Scratch
Sobel Operator: It is a discrete differentiation gradient-based operator. It computes the gradient approximation of image intensity function for image edge detection. At the pixels of an image, the Sobel operator produces either the normal to a vector or the corresponding gradient vector. It uses two 3 x 3 kernels or masks which are convolved with
3 min read
MATLAB - Image Edge Detection using Robert Operator from Scratch
Robert Operator: This gradient-based operator computes the sum of squares of the differences between diagonally adjacent pixels in an image through discrete differentiation. Then the gradient approximation is made. It uses the following 2 x 2 kernels or masks – [Tex]\[M_{x}=\left[\begin{array}{ccc}1 & 0 \\ 0 & -1\end{array}\right] \quad M_{
2 min read
Article Tags :
  翻译: