Lesson 41/100

Tutorials MongoDB Tutorial

Single Field Indexes

Single Field Indexes: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.

On this page

MongoDB Tutorial · Lesson 41 of 100

Single Field Indexes

Foundations & CRUD ✓Queries & SchemaAggregation & ScaleAtlas & Projects

Queries & Schema · 2 — Design · ~6 min · MongoDB — Indexing & Performance

What is this?

A single field index stores one field’s values in sorted order so equality and range queries avoid scanning the whole collection. MongoDB always indexes _id.

Why should you care?

find({ email }) on millions of users without an index is a full collection scan — login and password-reset APIs will time out.

See it live — copy this example

Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.

db.users.insertMany([
  { email: "a@nosqlverse.dev", city: "Pune" },
  { email: "b@nosqlverse.dev", city: "Goa" }
])
db.users.createIndex({ email: 1 }, { unique: true })
db.users.find({ email: "a@nosqlverse.dev" }).explain("executionStats")
db.users.getIndexes()

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • createIndex on email builds an ascending index and enforces uniqueness.
  • explain should show IXSCAN.
  • getIndexes lists _id_ and email_1.

Practice next

  1. Create the unique email index.
  2. Find by email and inspect explain.
  3. Try inserting a duplicate email — expect error.
  4. Index city: 1 and query city ranges alphabetically.
  5. Create a sparse index on phone.

Remember

Single-field indexes speed filters on that field. unique prevents duplicates. Verify with explain IXSCAN.

Login by email

Auth service finds users by email on every sign-in.

Outcome: Unique index gives speed and blocks duplicate accounts.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MongoDB.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MongoDB?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MongoDB application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MongoDB project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MongoDB Tutorial
Course syllabus

MongoDB Tutorial

MongoDB — Foundations
MongoDB — CRUD Operations
MongoDB — Query Operators
MongoDB — Schema Design
MongoDB — Indexing & Performance
MongoDB — Aggregation Pipelines
MongoDB — Replication & Sharding
MongoDB — Atlas & Security
MongoDB — Modern Features
MongoDB — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details