Can MongoDB auto increment?

MongoDB does not have a built-in auto-increment feature like traditional relational databases (e.g., MySQL's AUTO_INCREMENT). However, you can implement auto-increment functionality using different approaches depending on your use case. Below are the commonly used methods:

1. Using a Counter Collection

  • Create a separate collection to store the counters for each entity that requires auto-incremented IDs.
  • Increment the counter value whenever you insert a new document.

Example Implementation

// Define a counter collection db.counters.insertOne({ _id: "productId", sequence_value: 0 }); // Function to get the next sequence value function getNextSequence(sequenceName) { const result = db.counters.findOneAndUpdate( { _id: sequenceName }, { $inc: { sequence_value: 1 } }, { returnDocument: "after" } ); return result.value.sequence_value; } // Insert a new document with an auto-incremented ID const newId = getNextSequence("productId"); db.products.insertOne({ _id: newId, name: "Product A" });

Use Case

This method is suitable for scenarios where you need globally unique, incrementing IDs.

2. Using Application Logic

  • Generate auto-incremented IDs in your application code before inserting documents into MongoDB.
  • Maintain a counter in memory or a database.

Example in JavaScript (Node.js)

let counter = 0; function getAutoIncrementedId() { return ++counter; } // Use the function to insert documents db.products.insertOne({ _id: getAutoIncrementedId(), name: "Product B" });

Use Case

This approach is useful for simpler use cases where managing counters in the application is sufficient.

3. Using MongoDB Triggers (for Atlas Users)

If you use MongoDB Atlas, you can leverage database triggers to implement auto-increment logic.

  • Create a trigger that increments a counter whenever a document is inserted.

Example Trigger Logic

exports = async function(changeEvent) { const collection = context.services.get("mongodb-atlas").db("mydb").collection("counters"); const productId = await collection.findOneAndUpdate( { _id: "productId" }, { $inc: { sequence_value: 1 } }, { returnDocument: "after" } ); return productId.sequence_value; };

Use Case

This method is suitable for managed databases where you want to handle auto-incrementing within the database itself.

4. Using ObjectId

While not an auto-increment solution, MongoDB’s default _id field uses ObjectId, which generates unique values automatically. This eliminates the need for manual auto-incrementing in many use cases.

Example

db.products.insertOne({ name: "Product C" });

Summary

MongoDB does not natively support auto-increment, but you can implement it using:

  1. A counter collection.
  2. Application logic.
  3. MongoDB Atlas triggers.

Each method has its trade-offs, and the choice depends on your specific requirements. If globally unique and incrementing IDs are not mandatory, consider using MongoDB's default ObjectId.

TAGS
Behavioral Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the top 10 behavioral questions in an interview?
Top Stripe Behavioral Interview Questions (and How to Answer Them)
Stripe's behavioral round filters polished-but-vague answers fast. The questions mapped to Stripe's operating principles, and how to answer with evidence.
How to Answer: "Why Do You Want to Work at DoorDash?"
DoorDash screens for operator mindset and genuine marketplace interest, and every employee delivers orders a few times a year. How to answer with evidence, plus a sample and mistakes.
Why are I interested in Microsoft?
Why does Amazon use the STAR method?
Top Shopify Behavioral Interview Questions (and How to Answer Them)
Shopify's behavioral evaluation runs through the Life Story interview: slope over y-intercept, agency over circumstance. The questions, the traits, and how to prepare honestly.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.