MongoDB 8 · Indexes and performance

Create a compound index

Index two fields together, in the order you filter and sort by them.

db.orders.createIndex({ userId: 1, createdAt: -1 })

Official MongoDB 8 documentation

More indexes and performance commands

All indexes and performance commands · MongoDB 8 cheatsheet

Was this page helpful?