Recall & Review
beginner
What does the
createIndex method do in MongoDB?It creates an index on a collection to improve the speed of queries by organizing data for faster search.
Click to reveal answer
beginner
How do you create a simple ascending index on the field
name using createIndex?Use
db.collection.createIndex({ name: 1 }). The 1 means ascending order.Click to reveal answer
intermediate
What is the difference between ascending (1) and descending (-1) in
createIndex?Ascending (1) sorts the index from smallest to largest, descending (-1) sorts from largest to smallest. Both speed up queries but order matters for sorting.
Click to reveal answer
intermediate
What option would you use with
createIndex to ensure no duplicate values in the indexed field?Use the option
{ unique: true } to prevent duplicate values in the indexed field.Click to reveal answer
intermediate
Can
createIndex be used to create compound indexes? If yes, how?Yes. You pass multiple fields in the object, like
{ field1: 1, field2: -1 } to create a compound index on both fields.Click to reveal answer
What is the main purpose of the
createIndex method in MongoDB?✗ Incorrect
Indexes help MongoDB find data faster, improving query speed.
Which of the following is the correct syntax to create an ascending index on the field
age?✗ Incorrect
The correct syntax uses 1 for ascending order.
How do you create a unique index on the field
email?✗ Incorrect
The option { unique: true } enforces uniqueness.
What does a compound index do?
✗ Incorrect
Compound indexes speed up queries involving multiple fields.
What does the value -1 mean in
createIndex?✗ Incorrect
-1 specifies descending order for the index.
Explain how to create a unique ascending index on the field 'username' in MongoDB.
Think about the syntax: db.collection.createIndex({ field: order }, { unique: true })
You got /4 concepts.
Describe the benefits of using the createIndex method in MongoDB.
Focus on performance and data integrity.
You got /4 concepts.