Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Projection for Selecting Fields in MongoDB
📖 Scenario: You are managing a small online bookstore database. The books collection contains documents with details about each book, including title, author, year published, genre, and price.You want to learn how to retrieve only specific information from the database to make your queries faster and easier to read.
🎯 Goal: Build a MongoDB query that uses projection to select only the title and author fields from the books collection.
📋 What You'll Learn
Create a books collection with 3 book documents having fields: title, author, year, genre, and price.
Create a variable called projection that selects only the title and author fields.
Write a MongoDB query using find() on books with the projection variable.
Add a final line to execute the query and store the results in a variable called selectedBooks.
💡 Why This Matters
🌍 Real World
Selecting only needed fields from a database makes queries faster and reduces data transfer, which is important for web apps and APIs.
💼 Career
Database developers and backend engineers often use projection to optimize queries and improve application performance.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with exactly these 3 documents: { title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937, genre: "Fantasy", price: 15 }, { title: "1984", author: "George Orwell", year: 1949, genre: "Dystopian", price: 12 }, { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960, genre: "Fiction", price: 10 }.
MongoDB
Hint
Use an array of objects with the exact field names and values given.
2
Create the projection variable to select fields
Create a variable called projection and set it to an object that includes title and author fields with value 1 to include them in the query result.
MongoDB
Hint
Use { title: 1, author: 1 } to include only these fields.
3
Write the MongoDB find() query with projection
Write a query using db.books.find() with an empty filter object {} and the projection variable as the second argument. Assign this query to a variable called query.
MongoDB
Hint
Use db.books.find({}, projection) to select only the fields in projection.
4
Execute the query and store the results
Add a line to execute the query by calling toArray() and assign the result to a variable called selectedBooks.
MongoDB
Hint
Use query.toArray() to get the results as an array.
Practice
(1/5)
1. What does projection do in a MongoDB query?
easy
A. It selects which fields to include or exclude in the query result.
B. It sorts the documents in the collection.
C. It updates the documents in the collection.
D. It deletes documents from the collection.
Solution
Step 1: Understand the purpose of projection
Projection in MongoDB is used to specify which fields to include or exclude in the output of a query.
Step 2: Differentiate projection from other operations
Sorting, updating, and deleting are different operations and not related to projection.
Final Answer:
It selects which fields to include or exclude in the query result. -> Option A
Quick Check:
Projection = Select fields [OK]
Hint: Projection picks fields to show, not to sort or update [OK]
Common Mistakes:
Confusing projection with sorting
Thinking projection updates data
Assuming projection deletes documents
2. Which of the following is the correct syntax to include only the name and age fields in a MongoDB find query?
easy
A. { name: 1, age: 1 }
B. { name: true, age: true }
C. { name: 'include', age: 'include' }
D. { name: 0, age: 0 }
Solution
Step 1: Recall projection syntax
In MongoDB, to include fields, you set them to 1 in the projection document.
Step 2: Check each option
{ name: 1, age: 1 } uses 1 correctly; { name: true, age: true } uses true which is valid in MongoDB; { name: 'include', age: 'include' } uses strings which is invalid; { name: 0, age: 0 } excludes fields instead of including.
Final Answer:
{ name: 1, age: 1 } -> Option A
Quick Check:
Include fields = 1 [OK]
Hint: Use 1 to include fields, not strings [OK]
Common Mistakes:
Using strings like 'include'
Using 0 to include fields
3. Given the collection documents:
{ _id: 1, name: "Alice", age: 25, city: "NY" }
{ _id: 2, name: "Bob", age: 30, city: "LA" }
What will be the result of the query db.collection.find({}, { name: 1, city: 1 })?