Section 12: Practice

🎯 Output Prediction Practice

Predict MongoDB Query Results - Master Mental Execution

πŸŽ“ About This Practice

Output prediction is a critical skill for MongoDB mastery. Instead of running queries, you'll predict what they return. This trains your mental model of how MongoDB processes queries, which is essential for interviews and debugging production issues.

πŸ’‘ How to Practice:
  • Read the data: Study the collection data carefully
  • Analyze the query: Break down what each operator does
  • Predict mentally: Write down your predicted output
  • Check answer: Click "Show Answer" to verify
  • Understand why: Read the explanation if you got it wrong

Your Progress

0
Attempted
50
Total Questions
0%

πŸ“ Basic Queries (1-10)

Start with fundamental find() queries and basic filtering.

Question 1 Easy

Collection: students

            [
            { _id: 1, name: "Alice", age: 20, grade: "A", city: "NYC" },
            { _id: 2, name: "Bob", age: 22, grade: "B", city: "LA" },
            { _id: 3, name: "Charlie", age: 20, grade: "A", city: "NYC" },
            { _id: 4, name: "Diana", age: 21, grade: "C", city: "Chicago" }
            ]

Query:

db.students.find({ age: 20 })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, name: "Alice", age: 20, grade: "A", city: "NYC" },
            { _id: 3, name: "Charlie", age: 20, grade: "A", city: "NYC" }
            ]
πŸ“– Explanation:

The query finds ALL documents where age equals 20. Both Alice and Charlie match this condition, so both documents are returned.

Question 2 Easy

Collection: students (same as above)

Query:

db.students.find({ city: "NYC" }, { name: 1, grade: 1, _id: 0 })

What will this query return?

βœ“ Correct Answer:

            [
            { name: "Alice", grade: "A" },
            { name: "Charlie", grade: "A" }
            ]
πŸ“– Explanation:

Projection { name: 1, grade: 1, _id: 0 } includes only name and grade fields, and explicitly excludes _id. Only students from NYC are returned.

Question 3 Easy

Collection: products

            [
            { _id: 1, name: "Laptop", price: 1200, stock: 5 },
            { _id: 2, name: "Mouse", price: 25, stock: 50 },
            { _id: 3, name: "Keyboard", price: 75, stock: 0 },
            { _id: 4, name: "Monitor", price: 300, stock: 10 }
            ]

Query:

db.products.findOne({ stock: 0 })

What will this query return?

βœ“ Correct Answer:

{ _id: 3, name: "Keyboard", price: 75, stock: 0 }
πŸ“– Explanation:

findOne() returns only the FIRST matching document (not an array). Since only Keyboard has stock: 0, it returns that single document.

Question 4 Easy

Collection: products (same as above)

Query:

db.products.find().sort({ price: -1 }).limit(2)

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, name: "Laptop", price: 1200, stock: 5 },
            { _id: 4, name: "Monitor", price: 300, stock: 10 }
            ]
πŸ“– Explanation:

sort({ price: -1 }) sorts by price in descending order (highest first). limit(2) returns only the top 2 results: Laptop ($1200) and Monitor ($300).

Question 5 Easy

Collection: employees

            [
            { _id: 1, name: "John", dept: "IT", salary: 80000 },
            { _id: 2, name: "Sarah", dept: "HR", salary: 65000 },
            { _id: 3, name: "Mike", dept: "IT", salary: 90000 },
            { _id: 4, name: "Lisa", dept: "Finance", salary: 75000 }
            ]

Query:

db.employees.find({ dept: "IT" }).count()

What will this query return?

βœ“ Correct Answer:

2
πŸ“– Explanation:

count() returns the NUMBER of documents matching the query. John and Mike are both in IT department, so the count is 2.

Question 6 Medium

Collection: books

            [
            { _id: 1, title: "MongoDB Guide", author: "Smith", pages: 320 },
            { _id: 2, title: "Node.js Basics", author: "Jones", pages: 280 },
            { _id: 3, title: "React Cookbook", author: "Smith", pages: 450 },
            { _id: 4, title: "Python 101", author: "Chen", pages: 380 }
            ]

Query:

db.books.find({ author: "Smith" }).sort({ pages: 1 })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, title: "MongoDB Guide", author: "Smith", pages: 320 },
            { _id: 3, title: "React Cookbook", author: "Smith", pages: 450 }
            ]
πŸ“– Explanation:

First, find all books by Smith (2 books). Then sort by pages in ascending order (1 means ascending). MongoDB Guide (320 pages) comes before React Cookbook (450 pages).

Question 7 Easy

Collection: orders

            [
            { _id: 1, customer: "Alice", total: 150, status: "delivered" },
            { _id: 2, customer: "Bob", total: 200, status: "pending" },
            { _id: 3, customer: "Charlie", total: 150, status: "delivered" },
            { _id: 4, customer: "Diana", total: 300, status: "shipped" }
            ]

Query:

db.orders.find({ total: 150, status: "delivered" })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, customer: "Alice", total: 150, status: "delivered" },
            { _id: 3, customer: "Charlie", total: 150, status: "delivered" }
            ]
πŸ“– Explanation:

When multiple conditions are in the query object, it's an implicit AND. Documents must match BOTH total: 150 AND status: "delivered".

Question 8 Easy

Collection: movies

            [
            { _id: 1, title: "Inception", year: 2010, rating: 8.8 },
            { _id: 2, title: "Interstellar", year: 2014, rating: 8.6 },
            { _id: 3, title: "Tenet", year: 2020, rating: 7.5 },
            { _id: 4, title: "Dunkirk", year: 2017, rating: 7.9 }
            ]

Query:

db.movies.find().skip(2).limit(2)

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 3, title: "Tenet", year: 2020, rating: 7.5 },
            { _id: 4, title: "Dunkirk", year: 2017, rating: 7.9 }
            ]
πŸ“– Explanation:

skip(2) skips the first 2 documents (Inception and Interstellar). limit(2) then returns the next 2 documents (Tenet and Dunkirk). This is pagination!

Question 9 Easy

Collection: cars

            [
            { _id: 1, brand: "Toyota", model: "Camry", year: 2020 },
            { _id: 2, brand: "Honda", model: "Civic", year: 2021 },
            { _id: 3, brand: "Toyota", model: "Corolla", year: 2020 },
            { _id: 4, brand: "Ford", model: "Mustang", year: 2022 }
            ]

Query:

db.cars.distinct("brand")

What will this query return?

βœ“ Correct Answer:

[ "Toyota", "Honda", "Ford" ]
πŸ“– Explanation:

distinct() returns an array of unique values for the specified field. Even though Toyota appears twice, it only appears once in the result.

Question 10 Medium

Collection: inventory

            [
            { _id: 1, item: "Notebook", qty: 50, price: 2.5 },
            { _id: 2, item: "Pen", qty: 100, price: 1.0 },
            { _id: 3, item: "Eraser", qty: 75, price: 0.5 },
            { _id: 4, item: "Pencil", qty: 80, price: 0.75 }
            ]

Query:

db.inventory.find({ qty: { $exists: true }, price: { $exists: true } }).count()

What will this query return?

βœ“ Correct Answer:

4
πŸ“– Explanation:

$exists: true checks if a field exists. All 4 documents have both qty and price fields, so count returns 4. This query checks for complete documents.

πŸ”§ Query Operators (11-20)

Practice with comparison and logical operators.

Question 11 Medium

Collection: scores

            [
            { _id: 1, student: "Alice", score: 85 },
            { _id: 2, student: "Bob", score: 92 },
            { _id: 3, student: "Charlie", score: 78 },
            { _id: 4, student: "Diana", score: 95 },
            { _id: 5, student: "Eve", score: 88 }
            ]

Query:

db.scores.find({ score: { $gte: 85, $lt: 90 } })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, student: "Alice", score: 85 },
            { _id: 5, student: "Eve", score: 88 }
            ]
πŸ“– Explanation:

$gte: 85 means >= 85, $lt: 90 means < 90. This finds scores in range [85, 90). Alice (85) and Eve (88) match. Bob (92) and Diana (95) are too high, Charlie (78) is too low.

Question 12 Medium

Collection: products

            [
            { _id: 1, name: "Laptop", price: 1200, category: "Electronics" },
            { _id: 2, name: "Desk", price: 300, category: "Furniture" },
            { _id: 3, name: "Mouse", price: 25, category: "Electronics" },
            { _id: 4, name: "Chair", price: 150, category: "Furniture" }
            ]

Query:

db.products.find({ $or: [{ price: { $lt: 100 } }, { category: "Furniture" }] })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 2, name: "Desk", price: 300, category: "Furniture" },
            { _id: 3, name: "Mouse", price: 25, category: "Electronics" },
            { _id: 4, name: "Chair", price: 150, category: "Furniture" }
            ]
πŸ“– Explanation:

$or returns documents matching ANY condition. Mouse ($25) matches price < $100. Desk and Chair match category "Furniture". Laptop matches neither.

Question 13 Medium

Collection: users

            [
            { _id: 1, name: "Alice", age: 25, city: "NYC", active: true },
            { _id: 2, name: "Bob", age: 30, city: "LA", active: false },
            { _id: 3, name: "Charlie", age: 25, city: "NYC", active: true },
            { _id: 4, name: "Diana", age: 28, city: "Chicago", active: true }
            ]

Query:

db.users.find({ city: { $in: ["NYC", "LA"] }, active: true })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, name: "Alice", age: 25, city: "NYC", active: true },
            { _id: 3, name: "Charlie", age: 25, city: "NYC", active: true }
            ]
πŸ“– Explanation:

$in: ["NYC", "LA"] matches documents where city is NYC OR LA. Combined with active: true (AND condition), only Alice and Charlie match. Bob is in LA but not active.

Question 14 Medium

Collection: inventory

            [
            { _id: 1, item: "Apple", qty: 50 },
            { _id: 2, item: "Banana", qty: 30 },
            { _id: 3, item: "Orange", qty: 40 },
            { _id: 4, item: "Grape", qty: 25 }
            ]

Query:

db.inventory.find({ qty: { $ne: 30 } })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 1, item: "Apple", qty: 50 },
            { _id: 3, item: "Orange", qty: 40 },
            { _id: 4, item: "Grape", qty: 25 }
            ]
πŸ“– Explanation:

$ne (not equal) returns all documents where qty is NOT 30. Apple (50), Orange (40), and Grape (25) all match. Only Banana (30) is excluded.

Question 15 Hard

Collection: orders

            [
            { _id: 1, items: ["apple", "banana"], total: 15 },
            { _id: 2, items: ["orange"], total: 10 },
            { _id: 3, items: ["apple", "orange", "grape"], total: 25 },
            { _id: 4, items: ["banana", "grape"], total: 18 }
            ]

Query:

db.orders.find({ items: { $all: ["apple", "orange"] } })

What will this query return?

βœ“ Correct Answer:

            [
            { _id: 3, items: ["apple", "orange", "grape"], total: 25 }
            ]
πŸ“– Explanation:

$all requires the array to contain ALL specified elements. Only order 3 has both "apple" AND "orange" in its items array. Order 1 has apple but not orange. Order 2 has orange but not apple.

πŸ“ Practice Tip:

For operator questions, always read carefully: $gt (>), $gte (>=), $lt (<), $lte (<=), $ne (!=), $in (array contains), $nin (not in array), $all (array contains all).

✏️ Update Operations (21-30)

Predict the result of update operations.

Question 21 Medium

Collection: counter

{ _id: 1, count: 10, name: "visitor" }

Query:

db.counter.updateOne({ _id: 1 }, { $inc: { count: 5 } })

What will the document look like after this update?

βœ“ Correct Answer:

{ _id: 1, count: 15, name: "visitor" }
πŸ“– Explanation:

$inc increments the field value. count was 10, $inc: { count: 5 } adds 5, making it 15. Other fields remain unchanged.

Question 22 Medium

Collection: products

{ _id: 1, name: "Laptop", price: 1200, tags: ["electronics"] }

Query:

db.products.updateOne({ _id: 1 }, { $push: { tags: "computers" } })

What will the document look like after this update?

βœ“ Correct Answer:

{ _id: 1, name: "Laptop", price: 1200, tags: ["electronics", "computers"] }
πŸ“– Explanation:

$push appends an element to an array. "computers" is added to the end of the tags array. The array now has two elements.

Question 23 Medium

Collection: users

{ _id: 1, name: "Alice", age: 25, status: "active" }

Query:

db.users.updateOne({ _id: 1 }, { $unset: { status: "" } })

What will the document look like after this update?

βœ“ Correct Answer:

{ _id: 1, name: "Alice", age: 25 }
πŸ“– Explanation:

$unset removes the specified field from the document. The status field is completely removed. The value in $unset doesn't matter (can be "", 1, true, etc.).

πŸ“Š Aggregation Pipelines (31-40)

Predict aggregation results.

Question 31 Hard

Collection: sales

            [
            { _id: 1, item: "A", quantity: 5, price: 10 },
            { _id: 2, item: "B", quantity: 3, price: 15 },
            { _id: 3, item: "A", quantity: 2, price: 10 },
            { _id: 4, item: "C", quantity: 4, price: 20 }
            ]

Query:

            db.sales.aggregate([
            {
                $group: {
                _id: "$item",
                totalQty: { $sum: "$quantity" }
                }
            }
            ])

What will this aggregation return?

βœ“ Correct Answer:

            [
            { _id: "A", totalQty: 7 },
            { _id: "B", totalQty: 3 },
            { _id: "C", totalQty: 4 }
            ]
πŸ“– Explanation:

$group groups by item. For item "A", there are 2 documents (qty 5 and 2), so totalQty = 5 + 2 = 7. Item "B" has qty 3, item "C" has qty 4.

πŸŽ‰ Keep Practicing!

Output prediction is one of the best ways to internalize MongoDB behavior. The more you practice, the faster you'll be able to write correct queries on the first try!

Next Steps: Try writing the queries in MongoDB Shell to verify your predictions. The hands-on practice solidifies your learning!