0
0
MongoDBquery~5 mins

createIndex method in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo create a new collection
BTo speed up query performance
CTo backup the database
DTo delete documents
Which of the following is the correct syntax to create an ascending index on the field age?
Adb.collection.createIndex('age')
Bdb.collection.createIndex({ age: 'asc' })
Cdb.collection.createIndex({ age: 1 })
Ddb.collection.createIndex({ age: true })
How do you create a unique index on the field email?
Adb.collection.createIndex({ email: 1 }, { unique: true })
Bdb.collection.createIndex({ email: 1 }, { unique: false })
Cdb.collection.createIndex({ email: 1 }, { duplicate: false })
Ddb.collection.createIndex({ email: 1 })
What does a compound index do?
AIndexes only one field
BCreates a backup of indexes
CDeletes multiple indexes
DIndexes multiple fields together
What does the value -1 mean in createIndex?
ADescending order
BAscending order
CNo order
DUnique 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.