createIndex()

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Indexes and aggregation

What it does

Creates an index with key order and options.

Syntax

db.collection.createIndex(keys, options)

Use case

Support frequent filters and sorts.

createIndex(): practical example
db.orders.createIndex({ customerId: 1, createdAt: -1 })
💡 Explanation Creates an index with key order and options. When to use it Support frequent filters and sorts.
Key Takeaways
  • Creates an index with key order and options.
  • Typical use: Support frequent filters and sorts.