Indexing: Single, Compound & Text
Indexes improve the search speed of query filtering by building index keys structured as balanced B-Trees.
1 Creating Indexes
JavaScript — Index operations
# Create index on specific field (1 = ascending, -1 = descending)
db.users.createIndex({ email: 1 });
# Create a compound index on multiple fields
db.users.createIndex({ age: 1, created_at: -1 });
# List all active indexes in a collection
db.users.getIndexes();
2 Code Challenge
Challenge: Create a text index on a product collection covering name and description fields, and write a query searching for products matching the term "phone".