Sorting Documents with MongoDB sort Method
📖 Scenario: You are managing a small online bookstore database. You want to organize the books so customers can see them sorted by price, either from cheapest to most expensive or the other way around.
🎯 Goal: Build MongoDB queries that use the sort method to display books sorted by their price in ascending and descending order.
📋 What You'll Learn
Create a collection named
books with the exact documents provided.Create a variable
sortAscending that holds the sorting order for ascending by price.Create a variable
sortDescending that holds the sorting order for descending by price.Write a query using
find() and sort(sortAscending) to get books sorted by price ascending.Write a query using
find() and sort(sortDescending) to get books sorted by price descending.💡 Why This Matters
🌍 Real World
Sorting data is essential in databases to help users find information quickly, like showing products from cheapest to most expensive.
💼 Career
Database developers and data analysts often write queries to sort data efficiently for reports and user interfaces.
Progress0 / 4 steps