📖 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
1
Create the books collection with sample data
Create a variable called books and assign it an array with these 3 documents exactly: { title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937, price: 15.99, description: "A fantasy novel." }, { title: "1984", author: "George Orwell", year: 1949, price: 12.99, description: "A dystopian novel." }, { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960, price: 14.99, description: "A novel about racial injustice." }
MongoDB
Hint
Use an array of objects with the exact fields and values given.
2
Create the projection variable to select only title and author
Create a variable called projection and set it to an object that includes title and author with value 1 to include them in the query result.
MongoDB
Hint
Use 1 to include fields in MongoDB projection.
3
Write the query using find() with the projection
Write a query using db.books.find({}, projection) to get only the title and author fields from the books collection.
MongoDB
Hint
Use empty filter {} to select all documents and pass projection as second argument.
4
Convert the query result to an array
Add .toArray() to the query to get the results as an array and assign it to a variable called result.
MongoDB
Hint
Use .toArray() to get all documents from the cursor as an array.
Practice
(1/5)
1. What is the main purpose of using projection in a MongoDB query?
easy
A. To select only specific fields from documents to reduce data transfer
B. To sort the documents in ascending order
C. To delete documents from the collection
D. To update multiple documents at once
Solution
Step 1: Understand projection concept
Projection in MongoDB is used to specify which fields to include or exclude in the query result.
Step 2: Identify the purpose of projection
Projection reduces the amount of data sent by selecting only needed fields, improving speed and efficiency.
Final Answer:
To select only specific fields from documents to reduce data transfer -> Option A
Quick Check:
Projection = Select fields only [OK]
Hint: Projection picks fields to reduce data size [OK]
Common Mistakes:
Confusing projection with sorting
Thinking projection deletes documents
Assuming projection updates data
2. Which of the following is the correct syntax to include only the name and age fields in a MongoDB find query?
easy
A. db.collection.find({}, {name: 1, age: 1})
B. db.collection.find({name: 1, age: 1})
C. db.collection.find({}, {name: 0, age: 0})
D. db.collection.find({name: 0, age: 0})
Solution
Step 1: Recall find() method syntax
The first argument is the filter (empty {} means all documents), the second is the projection specifying fields to include or exclude.
Step 2: Identify correct projection for inclusion
Using 1 includes fields; so {name: 1, age: 1} includes only those fields.
Final Answer:
db.collection.find({}, {name: 1, age: 1}) -> Option A
What will be the output of this query? db.collection.find({}, {name: 1, _id: 0})
medium
A. [{"_id": 1, "name": "Alice"}, {"_id": 2, "name": "Bob"}]
B. [{"age": 25, "city": "NY"}, {"age": 30, "city": "LA"}]
C. [{"name": "Alice"}, {"name": "Bob"}]
D. [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]
Solution
Step 1: Analyze projection fields
The projection includes only name with 1 and excludes _id with 0, so only the name field appears without _id.
Step 2: Determine output documents
Each document will have only the name field, no _id or other fields.
Final Answer:
[{"name": "Alice"}, {"name": "Bob"}] -> Option C
Quick Check:
Projection includes name only, excludes _id [OK]
Hint: Set _id:0 to exclude _id field [OK]
Common Mistakes:
Forgetting _id is included by default
Expecting other fields to appear
Using 0 to include fields
4. Identify the error in this MongoDB query that tries to exclude the password field: db.users.find({}, {password: 1, email: 0})
medium
A. Projection must include _id explicitly
B. The filter argument is missing
C. The query is correct and will work as expected
D. You cannot mix including and excluding fields in projection except for _id
Solution
Step 1: Understand projection rules
MongoDB does not allow mixing inclusion (1) and exclusion (0) in the same projection except for the _id field.
Step 2: Analyze the given projection
The projection tries to include password (1) and exclude email (0) together, which is invalid.
Final Answer:
You cannot mix including and excluding fields in projection except for _id -> Option D
Quick Check:
Mixing 1 and 0 in projection = error [OK]
Hint: Include or exclude fields, not both (except _id) [OK]
Common Mistakes:
Mixing 1 and 0 in projection
Assuming _id must be included explicitly
Ignoring filter argument is optional
5. You want to retrieve only the title and author fields from a large books collection but exclude the _id field to reduce data size. Which query correctly achieves this and why?
hard
A. db.books.find({}, {title: 0, author: 0, _id: 1}) because excluding fields reduces data
B. db.books.find({}, {title: 1, author: 1, _id: 0}) because it includes needed fields and excludes _id
C. db.books.find({}, {title: 1, author: 1}) because _id is excluded by default
D. db.books.find({}, {title: 1, author: 0, _id: 0}) because mixing inclusion and exclusion is allowed
Solution
Step 1: Determine fields to include and exclude
You want to include title and author fields and exclude _id to reduce data size.
Step 2: Apply correct projection syntax
Including fields uses 1, excluding _id uses 0. Mixing inclusion and exclusion (except _id) is not allowed.
Step 3: Evaluate options
db.books.find({}, {title: 1, author: 1, _id: 0}) because it includes needed fields and excludes _id correctly includes title and author with 1 and excludes _id with 0. Others either exclude needed fields or mix inclusion/exclusion incorrectly.
Final Answer:
db.books.find({}, {title: 1, author: 1, _id: 0}) because it includes needed fields and excludes _id -> Option B
Quick Check:
Include fields with 1, exclude _id with 0 [OK]
Hint: Include needed fields with 1, exclude _id with 0 [OK]
Common Mistakes:
Assuming _id is excluded by default
Mixing inclusion and exclusion fields
Excluding needed fields by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.