Skip to content
Justice Tech School

Indexes

An index lets MongoDB find documents without scanning the collection. Create with createIndex, 1 or -1 for the direction. Compound indexes follow field order and unique indexes block duplicates.

Example
db.users.createIndex({ email: 1 }, { unique: true })
db.users.createIndex({ city: 1, age: -1 })
db.users.getIndexes()

Exercise

Unique index.

{ : true }

Quiz Test

What does an index speed up?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.