← All tutorials

GoCareerGo Tutorials

MongoDB Tutorial

Documents, collections, CRUD, aggregation, Atlas and driver connectivity.

Indexing in MongoDB

Indexes let MongoDB find documents without scanning the entire collection — critical for query performance at scale.

db.users.createIndex({ email: 1 });          // single-field, ascending
db.orders.createIndex({ userId: 1, date: -1 }); // compound index
db.users.getIndexes();
  • Every collection gets a default index on _id automatically
  • Compound indexes speed up queries that filter/sort on multiple fields together
  • Indexes speed up reads but add overhead to writes — don't over-index