0
0
MongoDBquery~30 mins

Excluding fields from results in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Excluding Fields from MongoDB Query Results
📖 Scenario: You are managing a small online bookstore database. You want to retrieve information about books but exclude some details like the internal _id field and the supplier information from the results to keep the output clean and focused.
🎯 Goal: Build a MongoDB query that fetches all books but excludes the _id and supplier fields from the returned documents.
📋 What You'll Learn
Create a collection named books with given book documents
Add a query filter variable to select all books
Write a MongoDB find query that excludes the _id and supplier fields
Complete the query with the correct projection to exclude fields
💡 Why This Matters
🌍 Real World
Excluding fields from query results helps keep data clean and protects sensitive information when sharing or displaying data.
💼 Career
Database developers and data analysts often need to control which fields are returned in queries to optimize performance and maintain privacy.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books that holds an array of three book documents with these exact fields and values: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925, supplier: "SupplierA" }, { title: "1984", author: "George Orwell", year: 1949, supplier: "SupplierB" }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960, supplier: "SupplierC" }.
MongoDB
Need a hint?

Use an array of objects with exact field names and values.

2
Create a query filter to select all books
Create a variable called query and set it to an empty object {} to select all documents in the books collection.
MongoDB
Need a hint?

Use an empty object {} to select all documents.

3
Write a find query excluding _id and supplier fields
Create a variable called projection and set it to an object that excludes the _id and supplier fields by setting them to 0. Then create a variable called result that simulates the MongoDB find method by filtering books with query and mapping to exclude the specified fields using projection.
MongoDB
Need a hint?

Set fields to 0 in projection to exclude them. Use map to remove those fields from each book.

4
Complete the query with the correct projection
Add a comment above the result variable that says // Query result excluding _id and supplier fields to clearly label the final output.
MongoDB
Need a hint?

Add a comment to label the final query result clearly.