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 & Schema → Aggregation & Scale → Atlas & 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.
What happened?
- createIndex on email builds an ascending index and enforces uniqueness.
- explain should show IXSCAN.
- getIndexes lists _id_ and email_1.
Practice next
- Create the unique email index.
- Find by email and inspect explain.
- Try inserting a duplicate email — expect error.
- Index city: 1 and query city ranges alphabetically.
- 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.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!