Bird
Raised Fist0
MongoDBquery~20 mins

Projection for reducing data transfer in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Projection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Projection to include specific fields
Given a MongoDB collection users with documents containing name, age, and email, what will be the output of this query?

db.users.find({}, {name: 1, email: 1, _id: 0})

Assume the collection has:
{"name": "Alice", "age": 30, "email": "alice@example.com"}
{"name": "Bob", "age": 25, "email": "bob@example.com"}
MongoDB
db.users.find({}, {name: 1, email: 1, _id: 0})
A[{"name": "Alice", "email": "alice@example.com"}, {"name": "Bob", "email": "bob@example.com"}]
B[{"name": "Alice", "age": 30, "email": "alice@example.com"}, {"name": "Bob", "age": 25, "email": "bob@example.com"}]
C[{"age": 30, "email": "alice@example.com"}, {"age": 25, "email": "bob@example.com"}]
D[{"name": "Alice"}, {"name": "Bob"}]
Attempts:
2 left
💡 Hint
Projection includes only the fields set to 1 and excludes _id when set to 0.
❓ query_result
intermediate
2:00remaining
Projection excluding fields
What will be the result of this MongoDB query?

db.products.find({}, {description: 0, _id: 0})

Given documents:
{"name": "Pen", "price": 1.5, "description": "Blue ink pen"}
{"name": "Notebook", "price": 3.0, "description": "200 pages"}
MongoDB
db.products.find({}, {description: 0, _id: 0})
A[{"price": 1.5}, {"price": 3.0}]
B[{"name": "Pen", "price": 1.5, "description": "Blue ink pen"}, {"name": "Notebook", "price": 3.0, "description": "200 pages"}]
C[{"name": "Pen", "price": 1.5}, {"name": "Notebook", "price": 3.0}]
D[{"name": "Pen"}, {"name": "Notebook"}]
Attempts:
2 left
💡 Hint
Setting a field to 0 excludes it from the result.
📝 Syntax
advanced
2:00remaining
Identify the invalid projection syntax
Which of the following MongoDB queries has invalid projection syntax and will cause an error?
Adb.orders.find({}, {item: true, quantity: true})
Bdb.orders.find({}, {item: 1, quantity: 1, _id: 0})
Cdb.orders.find({}, {item: 1, quantity: 0})
Ddb.orders.find({}, {item: 1, quantity: 1, _id: 1, price: 0})
Attempts:
2 left
💡 Hint
You cannot mix inclusion (1) and exclusion (0) in the same projection except for _id.
❓ optimization
advanced
2:00remaining
Optimizing data transfer with projection
You have a large collection logs with documents containing many fields. You only need the timestamp and level fields for your report. Which query best reduces data transfer?
Adb.logs.find({}, {timestamp: 1, level: 1, _id: 0})
Bdb.logs.find({}, {timestamp: 1, level: 1})
Cdb.logs.find({}, {})
Ddb.logs.find({}, {timestamp: 0, level: 0})
Attempts:
2 left
💡 Hint
Exclude _id if you don't need it to reduce data size further.
🧠 Conceptual
expert
3:00remaining
Understanding projection behavior with nested fields
Consider documents in employees collection with nested address field:
{"name": "John", "address": {"city": "NY", "zip": "10001"}, "age": 28}

What will be the output of this query?

db.employees.find({}, {"address.city": 1, _id: 0})
MongoDB
db.employees.find({}, {"address.city": 1, _id: 0})
A[{"address": {"city": "NY", "zip": "10001"}}]
B[{"address": {"city": "NY"}}]
C[{"address.city": "NY"}]
D[{"name": "John", "address": {"city": "NY"}}]
Attempts:
2 left
💡 Hint
Projection on nested fields returns the parent object with only the specified nested fields.

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

  1. Step 1: Understand projection concept

    Projection in MongoDB is used to specify which fields to include or exclude in the query result.
  2. Step 2: Identify the purpose of projection

    Projection reduces the amount of data sent by selecting only needed fields, improving speed and efficiency.
  3. Final Answer:

    To select only specific fields from documents to reduce data transfer -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify correct projection for inclusion

    Using 1 includes fields; so {name: 1, age: 1} includes only those fields.
  3. Final Answer:

    db.collection.find({}, {name: 1, age: 1}) -> Option A
  4. Quick Check:

    Include fields = 1 in second argument [OK]
Hint: Use 1 to include fields in projection [OK]
Common Mistakes:
  • Putting projection in the first argument
  • Using 0 to include fields
  • Omitting the empty filter {}
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 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

  1. 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.
  2. Step 2: Determine output documents

    Each document will have only the name field, no _id or other fields.
  3. Final Answer:

    [{"name": "Alice"}, {"name": "Bob"}] -> Option C
  4. 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

  1. Step 1: Understand projection rules

    MongoDB does not allow mixing inclusion (1) and exclusion (0) in the same projection except for the _id field.
  2. Step 2: Analyze the given projection

    The projection tries to include password (1) and exclude email (0) together, which is invalid.
  3. Final Answer:

    You cannot mix including and excluding fields in projection except for _id -> Option D
  4. 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

  1. Step 1: Determine fields to include and exclude

    You want to include title and author fields and exclude _id to reduce data size.
  2. Step 2: Apply correct projection syntax

    Including fields uses 1, excluding _id uses 0. Mixing inclusion and exclusion (except _id) is not allowed.
  3. 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.
  4. Final Answer:

    db.books.find({}, {title: 1, author: 1, _id: 0}) because it includes needed fields and excludes _id -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard