Create and Use Ascending and Descending Indexes in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to speed up searches on book titles and prices by creating indexes. You will create one index that sorts titles in ascending order and another index that sorts prices in descending order.
🎯 Goal: Build MongoDB indexes on the books collection: one index on title in ascending order and another index on price in descending order.
📋 What You'll Learn
Create a
books collection with three documents containing title and price fields.Create a variable called
titleIndex that defines an ascending index on the title field.Create a variable called
priceIndex that defines a descending index on the price field.Create the ascending index on
title using db.books.createIndex() with titleIndex.Create the descending index on
price using db.books.createIndex() with priceIndex.💡 Why This Matters
🌍 Real World
Indexes are used in real databases to speed up searches and sorting, just like an index in a book helps you find pages quickly.
💼 Career
Database administrators and developers create and manage indexes to optimize application performance and handle large data efficiently.
Progress0 / 4 steps