0
0
MongoDBquery~10 mins

Single field index in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Single field index
Create collection
Define index on one field
Insert documents
Query uses index
Faster search on that field
Return matching documents
This flow shows how creating an index on a single field helps MongoDB find documents faster when querying that field.
Execution Sample
MongoDB
db.users.createIndex({ age: 1 })
db.users.insertMany([
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Carol", age: 25 }
])
db.users.find({ age: 25 })
Create an index on the 'age' field, insert documents, then query documents where age is 25.
Execution Table
StepActionDetailsResult
1Create indexIndex on field 'age' ascendingIndex created on 'age'
2Insert documentsInsert Alice (25), Bob (30), Carol (25)3 documents added
3Query documentsFind documents where age = 25Uses index to find Alice and Carol
4Return resultsDocuments matching age=25[{name: 'Alice', age: 25}, {name: 'Carol', age: 25}]
5EndNo more stepsQuery complete
💡 Query completes after returning all documents with age 25 using the index
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Index on ageNoneCreatedCreatedUsed in queryUsed
Documents in collectionEmptyEmpty3 documents3 documents3 documents
Query resultNoneNoneNone2 documents found2 documents returned
Key Moments - 2 Insights
Why does the query run faster after creating the index?
Because the index on 'age' lets MongoDB quickly locate documents with age 25 without scanning all documents, as shown in execution_table step 3.
Does the index store the whole document?
No, the index stores the values of the 'age' field and pointers to documents, so it is smaller and faster to search, as implied in step 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3?
AIndex is created
BDocuments are inserted
CDocuments with age 25 are found
DQuery ends
💡 Hint
Check the 'Result' column in row with Step 3 in execution_table
At which step are documents added to the collection?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for document insertion
If we did not create the index, how would the query step change?
AQuery would scan all documents (slower)
BQuery would use the index anyway
CQuery would fail
DQuery would return no results
💡 Hint
Indexes speed up queries by avoiding full scans, see key_moments explanation
Concept Snapshot
Single field index in MongoDB:
- Created with createIndex({field: 1})
- Speeds up queries filtering on that field
- Index stores field values + pointers
- Query uses index to find matches fast
- Improves read performance without changing data
Full Transcript
This visual execution shows how creating a single field index in MongoDB works. First, an index is created on the 'age' field. Then, documents are inserted into the collection. When a query searches for documents where age equals 25, MongoDB uses the index to quickly find matching documents instead of scanning all documents. The result is faster query performance. The index stores only the 'age' values and pointers to documents, not the whole documents. This example helps beginners see how indexes improve search speed step-by-step.