In programming, we tend to use the terms counter, increment, and decrement operators to add or subtract a certain value for a variable or a loop count. Just like another programming language, MongoDB came up with it including the “$inc” operator to add a certain value to the already existing value at a field. The “$inc” operator doesn’t only increase the value but can also decrease the value. The value to be added to a certain field can be positive or negative according to the situation – positive value to add and negative to subtract. This article helps you out in using the “$inc” operator within the MongoDB shell commands.
Generate a Collection in MongoDB
The first step towards the use of the “$inc” operator in MongoDB is to have some data in the database in which the “$inc” operator can be applied. Therefore, we should have a database collection in our MongoDB system to start using it. For this, we generate an empty collection name, “Test”, after using the “createCollection” function in the MongoDB console.
The “ok: 1” acknowledgment shows that the empty collection “Test” is now generated. To check this out, try out the “find()” function query along with the name of a collection as in the following illustration. Right now, it shows that this collection has no records.
Add Records to the Collection
The second step towards the use of the “$inc” operator in MongoDB is to add the records to a newly generated collection, “Test”. For this, we cast off the insertMany() function query which is preceded by the name of a collection, “Test”. A total of 3 records are added to this collection. Each of its records contains 3 single fields – id Title, Age, and 1 document-format field. The fields within a field like “Score” and “Position” reside in the “Data” field.
... { id: 2, Title: "Bella", Age: 21, Data: {Score: 498, Position: 1} },
... { id: 3, Title: "Lia", Age: 22, Data: { Score: 442, Position: 3 } } ])
The previously-attached image output shows that the insertion takes place successfully at our end. Now, it’s time to display those added records. To display it, MongoDB’s “find” function query which is preceded by the name of the collection, “Test”, helps us as we already used it in the previous example. The 3 records of the “Test” collection are displayed as shown in the following:
Example 1: Modify a Single Field in a Single Record
Starting with this example, we use the “$inc” operator to sum a value in a single field of any record. To do that, you must try the “updateOne” function within the “db” instruction followed by the “Test” collection. This command should start with the identification of a record number through its specific field, the “id: 1”. After this, another set starts with the use of the “$inc” operator which is applied to a certain single field with some numerical value. This numerical value is incremented in the field’s actual value which is “Age”. The execution of this query displays the acknowledgment.
Let’s fetch the record “1” from the “Test” collection using the “Id:1” as identification in the “find” function of the “db” query. The output demonstrates that the “Age” field is successfully incremented by a “-1” value for a record of “1”, i.e. Age = 22 + (-1) = 21.
Example 2: Modify Multiple Fields in a Single Record
Let’s use the “$inc” operator on the “Test” collection to increment the multiple field values including the document-type field. Remember that these increments on multiple fields only applies to a single record in a “Test” collection. So, the updateOne() function is again utilized to update the record number “2” from the “Test” collection as identified by “id: 1”. The “$inc” operator is applied to the single-format “Age” field to add its previous value with “-1”. Also, the “Score” and “Position” fields within the document-format “Data” field are incremented by “580” and “2”, respectively, as per the output using the “dot” product.
After modifying the record “2” of the “Test” collection, we take a good look at it separately via the “find” function which is used in the “db” instruction by mentioning the “id: 2” for a record to be identified. The value of a single record, “Age”, along with the Score and Position fields within a nested “Data” field is successfully incremented by the “$inc” operator.
Example 3: Modify Multiple Fields in Multiple Records
After modifying the single and multiple field records in a single record, let us now modify the multiple fields of multiple records in one collection through the use of the “$inc” operator. For this, we modify the “update” query. Instead of using the “updateOne” function, we cast off the “updateMany” function to modify more than 1 record of a “Test” collection. This “db” instruction can use the “id” field for multiple records or simply just leave the curly brackets “{}” empty to modify all the records of the collection. The “$inc” operator modifies the Age, Score, and Position fields of all the 3 records in a row for the “Test” collection. The modified count “3” shows the acknowledgment after the execution of this query.
After modifying the multiple fields of multiple records after applying the “$inc” operator, it’s our right to have a look at these modified records through the “db” instruction. After displaying all the 3 records of the “Test” collection, we find that all the previously-specified fields are incremented by specific values for all the 3 records.
Conclusion
This article includes the comparison of increment operators of other programming languages and the “$inc” operator of MongoDB in its introductory paragraph. After trying certain query examples in the MongoDB shell, we illustrated the use of the “$inc” operator for more than 1 purpose. The first two instances are about the increment of a single field and multiple fields (including a document-format field) within a single record. While the last illustration helps you increment many fields in multiple records of any database in the simplest way possible.