arrow_back
Back

MongoDB indexes: single-field and compound

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

List indexes on a collection:

db.posts.getIndexes()

Single-field index

Create an ascending index on score:

db.records.createIndex({ score: 1 })

The value in the index specification is the index direction: 1 ascending, -1 descending.

That index supports queries that filter on score, for example:

db.records.find({ score: 2 })
db.records.find({ score: { $gt: 10 } })

Compound index

db.products.createIndex({ item: 1, stock: 1 })
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

MongoDB aggregation framework

arrow_forward