

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set limit to $inc in MongoDB?
To set limit to $inc, use the below syntax −
db.yourCollectionName.update({yourFieldName : {$lt : yourValue}}, {$inc : {yourFieldName : yourIncrementValue}},false,true);
Let us first create a collection with documents −
> db.limitIncrementDemo.insertOne({"StudentId":101,"StudentScore":95}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2ce9eb64f4b851c3a13c3") } > db.limitIncrementDemo.insertOne({"StudentId":102,"StudentScore":55}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea0b64f4b851c3a13c4") } > db.limitIncrementDemo.insertOne({"StudentId":103,"StudentScore":67}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea1b64f4b851c3a13c5") } > db.limitIncrementDemo.insertOne({"StudentId":104,"StudentScore":56}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea3b64f4b851c3a13c6") } > db.limitIncrementDemo.insertOne({"StudentId":105,"StudentScore":79}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea4b64f4b851c3a13c7") }
Following is the query to display all documents from a collection with the help of find() method −
> db.limitIncrementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2ce9eb64f4b851c3a13c3"), "StudentId" : 101, "StudentScore" : 95 } { "_id" : ObjectId("5cd2cea0b64f4b851c3a13c4"), "StudentId" : 102, "StudentScore" : 55 } { "_id" : ObjectId("5cd2cea1b64f4b851c3a13c5"), "StudentId" : 103, "StudentScore" : 67 } { "_id" : ObjectId("5cd2cea3b64f4b851c3a13c6"), "StudentId" : 104, "StudentScore" : 56 } { "_id" : ObjectId("5cd2cea4b64f4b851c3a13c7"), "StudentId" : 105, "StudentScore" : 79 }
Following is the query to set limit to $inc −
> db.limitIncrementDemo.update({StudentScore : {$lt : 75}}, {$inc : {StudentScore : 10}},false,true); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Let us check all the documents from the above collection −
> db.limitIncrementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2ce9eb64f4b851c3a13c3"), "StudentId" : 101, "StudentScore" : 95 } { "_id" : ObjectId("5cd2cea0b64f4b851c3a13c4"), "StudentId" : 102, "StudentScore" : 65 } { "_id" : ObjectId("5cd2cea1b64f4b851c3a13c5"), "StudentId" : 103, "StudentScore" : 77 } { "_id" : ObjectId("5cd2cea3b64f4b851c3a13c6"), "StudentId" : 104, "StudentScore" : 66 } { "_id" : ObjectId("5cd2cea4b64f4b851c3a13c7"), "StudentId" : 105, "StudentScore" : 79 }
- Related Questions & Answers
- How to query MongoDB with a LIMIT?
- How to set heap watch limit in android?
- MongoDB Aggregate to limit the number of records
- How to set minimum size limit for a JFrame in Java
- MongoDB query to insert but limit the total records
- In MongoDB will limit() increase query speed?
- MongoDB query to limit the returning values of a field?
- How do you limit an array sub-element in MongoDB?
- MongoDB GroupBy to set status
- How to limit the amount of characters returned from a field in a MongoDB?
- MongoDB Limit fields and slice projection together?
- How to limit the amount of characters returned from a field in a MongoDB query?
- Limit number of values in a field using MongoDB?
- Limit number of values in a field with MongoDB?
- How to update a Timestamp and set to current date in MongoDB?
Advertisements