What if you could find any book instantly, no matter how big the library is?
Why createIndex method in MongoDB? - Purpose & Use Cases
Imagine you have a huge collection of books in a library. You want to find all books by a certain author, but you have to look through every single book one by one.
Searching this way is very slow and tiring. It takes a lot of time and you might make mistakes or miss some books because you have to check everything manually.
The createIndex method helps by organizing the data so you can quickly find what you need without checking every item. It's like having a special list that points you directly to the books by that author.
db.books.find({author: 'John Doe'}) // scans all booksdb.books.createIndex({author: 1});
db.books.find({author: 'John Doe'}) // fast search using indexIt makes searching large collections fast and efficient, saving time and effort.
A bookstore uses createIndex on the "title" field so customers can quickly find books by name without waiting.
Manual searching is slow and error-prone.
createIndex organizes data for quick access.
Indexes speed up queries and improve performance.
Practice
createIndex method in MongoDB?Solution
Step 1: Understand what
ThecreateIndexdoescreateIndexmethod organizes data to make searches faster.Step 2: Identify the correct purpose
It does not delete or update documents, nor create collections, only speeds up queries.Final Answer:
To speed up queries by creating an index on a field -> Option AQuick Check:
createIndexspeeds up queries [OK]
- Confusing createIndex with delete or update operations
- Thinking createIndex creates collections
- Assuming createIndex modifies documents
age in MongoDB?Solution
Step 1: Recall syntax for createIndex
The method requires an object with field names as keys and 1 or -1 as values for ascending or descending.Step 2: Identify correct ascending index syntax
{ age: 1 } means ascending index on age. Other options are either descending (-1) or invalid syntax.Final Answer:
db.collection.createIndex({ age: 1 }) -> Option AQuick Check:
Ascending index uses 1 [OK]
- Using -1 for ascending index
- Missing curly braces around field and order
- Passing field without object braces
db.users.createIndex({ email: 1 }, { unique: true }), what will happen if you try to insert two documents with the same email value?Solution
Step 1: Understand unique index effect
Unique index prevents duplicate values in the indexed field.Step 2: Predict behavior on duplicate insert
Inserting a duplicateemailwill cause an error and reject the second insert.Final Answer:
The second insert will fail due to duplicate key error -> Option CQuick Check:
Unique index blocks duplicates [OK]
- Assuming duplicates are allowed with unique index
- Thinking MongoDB auto-updates duplicates
- Believing index is ignored on duplicates
db.products.createIndex({ price: 'ascending' })Solution
Step 1: Check index order values
MongoDB requires 1 for ascending or -1 for descending, not strings like 'ascending'.Step 2: Identify error cause
Using 'ascending' causes syntax error because it's not a valid order value.Final Answer:
The index order must be 1 or -1, not 'ascending' -> Option DQuick Check:
Index order must be 1 or -1 [OK]
- Using string values instead of 1 or -1
- Thinking createIndex can't index numbers
- Confusing collection name with index fields
lastName (ascending) and age (descending) fields, ensuring no two documents have the same combination. Which command is correct?Solution
Step 1: Understand compound index syntax
Compound index uses an object with multiple fields and their order: 1 for ascending, -1 for descending.Step 2: Check uniqueness and order
db.collection.createIndex({ lastName: 1, age: -1 }, { unique: true }) matches ascending lastName (1), descending age (-1), and unique true to prevent duplicates.Step 3: Verify other options
db.collection.createIndex({ lastName: -1, age: 1 }, { unique: false }) reverses order and is not unique; C uses ascending age instead of descending; D uses invalid string values.Final Answer:
db.collection.createIndex({ lastName: 1, age: -1 }, { unique: true }) -> Option BQuick Check:
Compound unique index uses correct order and unique:true [OK]
- Using strings 'asc'/'desc' instead of 1/-1
- Setting unique:false when uniqueness is needed
- Mixing ascending and descending order incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
