ToollessToolless

Indexing

Create and manage indexes for better query performance

Overview

Indexes improve query performance by creating efficient lookup structures for frequently queried fields.

Creating Indexes

// Single field index
await users.createIndex({ email: 1 });

// Compound index
await users.createIndex({ lastName: 1, firstName: 1 });

// Unique index
await users.createIndex({ email: 1 }, { unique: true });

Index Direction

  • 1 = ascending order
  • -1 = descending order

For single-field indexes, direction rarely matters. For compound indexes, match your query's sort order.

Unique Indexes

Prevent duplicate values:

await users.createIndex({ email: 1 }, { unique: true });

// This will fail
await users.insertOne({ email: "alice@example.com" });
await users.insertOne({ email: "alice@example.com" }); // Error!

Listing Indexes

const indexes = await users.listIndexes();
console.log(indexes);

Dropping Indexes

await users.dropIndex("email_1");

When to Index

Index fields that are:

  • Frequently queried
  • Used in sort operations
  • Used in unique constraints

Avoid indexing fields that:

  • Rarely appear in queries
  • Have low cardinality (few unique values)
  • Change frequently

Index Strategy

// For query: find({ role: 'admin' }).sort({ createdAt: -1 })
await users.createIndex({ role: 1, createdAt: -1 });

// For query: find({ email: 'x' })
await users.createIndex({ email: 1 });

Performance Impact

OperationWithout IndexWith Index
Find by fieldO(n) scanO(log n) lookup
SortO(n log n)O(n)
InsertO(1)O(log n)

Best Practices

  1. Create indexes based on actual query patterns
  2. Use compound indexes for multi-field queries
  3. Monitor query performance
  4. Remove unused indexes
  5. Index fields used in filters before sort fields

On this page