Projection for Reducing Data Transfer in MongoDB
📖 Scenario: You work for a small online bookstore. The database has a collection called books with many details about each book, including title, author, year, price, and description. You want to show a list of books on the website but only need the title and author to reduce the amount of data sent from the database.
🎯 Goal: Build a MongoDB query that uses projection to return only the title and author fields from the books collection, excluding all other fields.
📋 What You'll Learn
Create a
books collection with 3 documents containing title, author, year, price, and description fields.Create a variable called
projection that includes only title and author fields.Write a query using
find() on books with the projection to get only those fields.Add a final command to convert the query result to an array.
💡 Why This Matters
🌍 Real World
Projection helps reduce the amount of data sent from the database to the application, making web pages load faster and saving bandwidth.
💼 Career
Database developers and backend engineers use projection to optimize queries and improve application performance.
Progress0 / 4 steps