
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Auto increment in MongoDB to store sequence of Unique User ID?
To auto increment in MongoDB to store a sequence of unique user id, let us create a collection which contains information about last sequence values of all documents.
Let us first create a collection. The query to create a collection which is as follows −
> db.createSequenceDemo.insertOne({_id:"SID",S_Value:0}); { "acknowledged" : true, "insertedId" : "SID" }
Now, we will create a function that will generate an auto increment in MongoDB to store sequence. The query is as follows −
> function nextSequence(s) { ... var sd = db.createSequenceDemo.findAndModify({ ... query:{_id: s }, ... update: {$inc:{S_Value:1}}, ... new:true ... }); ... return sd.S_Value; ... }
Let us create a collection with some documents and call the above function in order to generate a sequence of unique user id.
The query to create a collection with a document is as follows −
> db.checkSequenceDemo.insertOne({"StudentId":nextSequence("SID"),"StudentName":"Larry","StudentMathMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f61008d10a061296a3c40") } > db.checkSequenceDemo.insertOne({"StudentId":nextSequence("SID"),"StudentName":"Mike","StudentMathMarks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f61118d10a061296a3c41") } > db.checkSequenceDemo.insertOne({"StudentId":nextSequence("SID"),"StudentName":"Sam","StudentMathMarks":67}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f611d8d10a061296a3c42") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.checkSequenceDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7f61008d10a061296a3c40"), "StudentId" : 1, "StudentName" : "Larry", "StudentMathMarks" : 78 } { "_id" : ObjectId("5c7f61118d10a061296a3c41"), "StudentId" : 2, "StudentName" : "Mike", "StudentMathMarks" : 89 } { "_id" : ObjectId("5c7f611d8d10a061296a3c42"), "StudentId" : 3, "StudentName" : "Sam", "StudentMathMarks" : 67 }
Look at the field “StudentId” which is auto incremented by 1
- Related Articles
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to get the next auto-increment id in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to set different auto-increment ids for two tables with a user-defined variable?
- How to change auto increment number in MySQL?
- Passing NULL to MySQL for auto increment?
- How to auto-increment the property of a JSONObject in Java?
- Minimum Increment to Make Array Unique in C++
- Change the Auto Increment counter in MySQL?
- How to auto-increment value of tables to lower value in MySQL?
- How to set initial value and auto increment in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- How to change auto increment number in the beginning in MySQL?
- How to increment a field in MongoDB?
