I am making a analytics system, the API call would provide a Unique User ID, but it's not in sequence and too sparse.

I need to give each Unique User ID an auto increment id to mark a analytics datapoint in a bitarray/bitset. So the first user encounters would corresponding to the first bit of the bitarray, second user would be the second bit in the bitarray, etc.

So is there a solid and fast way to generate incremental Unique User IDs in MongoDB?

Solution 1

As selected answer says you can use findAndModify to generate sequential IDs.

But I strongly disagree with opinion that you should not do that. It all depends on your business needs. Having 12-byte ID may be very resource consuming and cause significant scalability issues in future.

I have detailed answer here.

Solution 2

You can, but you should not

Each object in mongo already has an id, and they are sortable in insertion order. What is wrong with getting collection of user objects, iterating over it and use this as incremented ID? Er go for kind of map-reduce job entirely

Solution 3

I know this is an old question, but I shall post my answer for posterity...

It depends on the system that you are building and the particular business rules in place.

I am building a moderate to large scale CRM in MongoDb, C# (Backend API), and Angular (Frontend web app) and found ObjectId utterly terrible for use in Angular Routing for selecting particular entities. Same with API Controller routing.

The suggestion above worked perfectly for my project.

 "name":"John Doe",
    "[email protected]",
    "[email protected]"

The reason it is perfect for my case, but not all cases is that as the above comment states, if you delete 3 records from the collection, you will get collisions.

My business rules state that due to our in house SLA's, we are not allowed to delete correspondence data or clients records for longer than the potential lifespan of the application I'm writing, and therefor, I simply mark records with an enum "Status" which is either "Active" or "Deleted". You can delete something from the UI, and it will say "Contact has been deleted" but all the application has done is change the status of the contact to "Deleted" and when the app calls the respository for a list of contacts, I filter out deleted records before pushing the data to the client app.

Therefore, db.collection.find().count() + 1 is a perfect solution for me...

It won't work for everyone, but if you will not be deleting data, it works fine.


latest versions of pymongo:

db.contacts.count() + 1

Solution 4

I had a similar issue, namely I was interested in generating unique numbers, which can be used as identifiers, but doesn't have to. I came up with the following solution. First to initialize the collection:

fun create(mongo: MongoTemplate) {
                .insertOne(Document(mapOf("_id" to "globalCounter", "sequenceValue" to 0L)))

An then a service that return unique (and ascending) numbers:

class IdCounter(val mongoTemplate: MongoTemplate) {

    companion object {
        const val collection = "sequence"

    private val idField = "_id"
    private val idValue = "globalCounter"
    private val sequence = "sequenceValue"

    fun nextValue(): Long {
        val filter = Document(mapOf(idField to idValue))
        val update = Document("\$inc", Document(mapOf(sequence to 1)))
        val updated: Document = mongoTemplate.db.getCollection(collection).findOneAndUpdate(filter, update)!!
        return updated[sequence] as Long

I believe that id doesn't have the weaknesses related to concurrent environment that some of the other solutions may suffer from.

Solution 5

First Record should be add

"_id" = 1    in your db

$database = "demo";
$collections ="democollaction";
echo getnextid($database,$collections);

function getnextid($database,$collections){

     $m = new MongoClient();
    $db = $m->selectDB($database);
    $cursor = $collection->find()->sort(array("_id" => -1))->limit(1);
    $array = iterator_to_array($cursor);

    foreach($array as $value){

        return $value["_id"] + 1;


Solution 6

// await collection.insertOne({ autoIncrementId: 1 });
const { value: { autoIncrementId } } = await collection.findOneAndUpdate(
  { autoIncrementId: { $exists: true } },
    $inc: { autoIncrementId: 1 },
return collection.insertOne({ id: autoIncrementId, });

Solution 7

// First check the table length

const data = await table.find()
if(data.length === 0){
   const id = 1
   // then post your query along with your id  
   // find last item and then its id
   const length = data.length
   const lastItem = data[length-1]
   const lastItemId = // or { id } = lastItem
   const id = lastItemId + 1
   // now apply new id to your new item
   // even if you delete any item from middle also this work

Solution 8

this could be another approach

const mongoose = require("mongoose");

const contractSchema = mongoose.Schema(
    account: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
    idContract: {
      type: Number,
      default: 0,
  { timestamps: true }

contractSchema.pre("save", function (next) {
  var docs = this;
    .model("contract", contractSchema)
    .countDocuments({ account: docs.account }, function (error, counter) {
      if (error) return next(error);
      docs.idContract = counter + 1;

module.exports = mongoose.model("contract", contractSchema);