MONGODB Tutorial



MongoDB INDEXING


⚡ MongoDB Indexing

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.

🔹 Creating a Single Field Index

This creates an index on a specific field.

db.users.createIndex({ name: 1 })   // 1 for ascending, -1 for descending
  

🔹 Creating a Compound Index

Indexes that cover multiple fields.

db.users.createIndex({ name: 1, age: -1 })
  

🔹 Viewing Indexes

Use this to list all indexes in a collection:

db.users.getIndexes()
  

🔹 Dropping an Index

Use this to remove a specific index:

db.users.dropIndex({ name: 1 })
  
Note: Indexes can speed up queries, but too many indexes may slow down insert and update operations and consume more storage.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review