Indexes in MongoDB are used to improve the performance of search queries. Without indexes, MongoDB must scan every document in a collection to find matches.
This creates an index on a specific field.
db.users.createIndex({ name: 1 }) // 1 for ascending, -1 for descending
Indexes that cover multiple fields.
db.users.createIndex({ name: 1, age: -1 })
Use this to list all indexes in a collection:
db.users.getIndexes()
Use this to remove a specific index:
db.users.dropIndex({ name: 1 })
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!