MongoDB Field Update Operator - $inc
Description
In MongoDB, the $inc operator is used to increment the value of a field by a specified amount. The $inc operator adds as a new field when the specified field does not exist, and sets the field to the specified amount. The $inc accepts positive and negative value as an incremental amount.
Syntax:
{ $inc: { <field1>: <amount1>, ... } }
Parameters:
Name | Description |
---|---|
field1 | name of the column or field |
amount1 | incremental value |
Our database name is 'myinfo' and our collection name is "items". Here, is the collection bellow.
Sample collection "items"
{
"_id" : 1,
"item_id" : "I001",
"comp_id" : "C001",
"qty" : 25,
"prate" : 30,
"srate" : 35,
"mrp" : 40
}
{
"_id" : 2,
"item_id" : "I001",
"comp_id" : "C002",
"qty" : 30,
"prate" : 32,
"srate" : 37,
"mrp" : 40
}
Example of field update operator - $inc
If we want to increment the value of qty by 10 for the first matching document in the collection items for item_id is I001 the following mongodb statement can be used:
> db.items.update( { item_id: "I001" },{ $inc: { qty: 10 }});
Here in the above example-
- The qty for the first matching document for the condition item_id is I001 have been updated and the qty became 35 from 25 and the rest remains same.
To see the updated output -
> db.items.find().pretty();
N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.
Output of the command
{ "_id" : 1, "item_id" : "I001", "comp_id" : "C001", "qty" : 35, "prate" : 30, "srate" : 35, "mrp" : 40 } { "_id" : 2, "item_id" : "I001", "comp_id" : "C002", "qty" : 30, "prate" : 32, "srate" : 37, "mrp" : 40 }
Example of $inc operator with multi:true
If we want to add a new field like tax% with a specific value 2 for all the documents in the collection items for item_id is I001, following mongodb queries can be used -
> db.items.update( { "item_id": "I001" }, { $inc: { "tax%": 2 } }, { multi: true } );
Here in the above example-
- A new field tax% which does not exists have been added with a value of 2 for all documents which match the condition item_id is I001. Here "multi : true" have used to update all documents which matching the specified condition.
To see the updated output -
> db.items.find().pretty();
N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.
Output of the command:
{ "_id" : 1, "comp_id" : "C001", "item_id" : "I001", "mrp" : 40, "prate" : 30, "qty" : 35, "srate" : 35, "tax%" : 2 } { "_id" : 2, "comp_id" : "C002", "item_id" : "I001", "mrp" : 40, "prate" : 32, "qty" : 30, "srate" : 37, "tax%" : 2 }
Example of $inc operator on multiple fields
If we want to increment the tax% and mrp for all the matching documents against a condition the following mongodb command can be used -
> db.items.update( { "item_id": "I001" }, { $inc: { "tax%": 2,"mrp":-2 } }, { multi: true } );
Here in the above example-
- the $inc operator expression specifies 2 for tax% and -2 for mrp field to increase the value of tax% by 2 and decrease the mrp by 2 simultaneously. The "multi : true" have used to update all matching documents for the specified condition.
To see the updated output -
> db.items.find().pretty();
N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.
Output of the command:
{ "_id" : 1, "comp_id" : "C001", "item_id" : "I001", "mrp" : 38, "prate" : 30, "qty" : 35, "srate" : 35, "tax%" : 4 } { "_id" : 2, "comp_id" : "C002", "item_id" : "I001", "mrp" : 38, "prate" : 32, "qty" : 30, "srate" : 37, "tax%" : 4 }
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/mongodb-field-update-operator-$inc.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics