Bird
Raised Fist0
MongoDBquery~20 mins

$count accumulator in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB $count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Counting documents with $count accumulator
Given a MongoDB collection orders with documents representing customer orders, what will be the output of this aggregation pipeline?

[{ $match: { status: "completed" } }, { $count: "totalCompleted" }]
MongoDB
[{ $match: { status: "completed" } }, { $count: "totalCompleted" }]
A[ { "totalCompleted": 5 } ]
B[ { "totalCompleted": 0 } ]
C[ { "count": 5 } ]
D[]
Attempts:
2 left
💡 Hint
The $count stage returns a document with the field name you specify and the count of documents passed to it.
🧠 Conceptual
intermediate
1:30remaining
Understanding $count accumulator behavior
What does the $count accumulator stage do in a MongoDB aggregation pipeline?
ACounts the number of fields in each document and outputs the count per document.
BCounts the number of documents input to it and outputs a single document with the count.
CCounts the number of distinct values of a specified field.
DCounts the total size in bytes of all documents passed to it.
Attempts:
2 left
💡 Hint
Think about what $count outputs after processing all documents.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $count usage
Which of the following aggregation pipelines will cause a syntax error due to incorrect $count usage?
A[{ $count: "total" }]
B[{ $match: { status: "active" } }, { $count: "activeCount" }]
C[{ $group: { _id: null, count: { $sum: 1 } } }]
D[{ $count: { total: true } }]
Attempts:
2 left
💡 Hint
$count expects a string as its argument, not an object.
❓ optimization
advanced
2:30remaining
Optimizing count queries with $count
You want to count documents with a specific condition in a large MongoDB collection. Which pipeline is more efficient?
A[{ $group: { _id: "$status", count: { $sum: 1 } } }, { $match: { _id: "pending" } }]
B[{ $project: { status: 1 } }, { $count: "count" }]
C[{ $match: { status: "pending" } }, { $count: "pendingCount" }]
D[{ $match: { status: { $exists: true } } }, { $count: "count" }]
Attempts:
2 left
💡 Hint
Filtering early reduces the number of documents processed by $count.
🔧 Debug
expert
3:00remaining
Why does this $count pipeline return an empty array?
Consider this aggregation pipeline:

[{ $match: { age: { $gt: 100 } } }, { $count: "oldPeople" }]

It returns an empty array []. Why?
ANo documents match the condition, so $count outputs no documents.
B$count always returns an empty array if used after $match.
CThe field name in $count must be _id, otherwise it returns empty.
DThe $match stage is invalid and filters out all documents.
Attempts:
2 left
💡 Hint
Think about what happens if no documents pass the $match filter.

Practice

(1/5)
1. What does the $count accumulator do in a MongoDB aggregation pipeline?
easy
A. Counts the number of documents in each group
B. Sums the values of a field in each group
C. Finds the maximum value in each group
D. Calculates the average of a field in each group

Solution

  1. Step 1: Understand the purpose of $count

    The $count accumulator is used inside the $group stage to count how many documents belong to each group.
  2. Step 2: Compare with other accumulators

    Unlike $sum, $max, or $avg, $count does not operate on field values but counts documents.
  3. Final Answer:

    Counts the number of documents in each group -> Option A
  4. Quick Check:

    $count = document count [OK]
Hint: Remember: $count counts documents, no field needed [OK]
Common Mistakes:
  • Thinking $count sums field values
  • Using $count outside $group stage
  • Expecting $count to take arguments
2. Which of the following is the correct syntax to count documents per category using $count in a $group stage?
easy
A. { $group: { _id: "$category", total: { $count: "$price" } } }
B. { $group: { _id: "$category", total: { $count: "$category" } } }
C. { $group: { _id: "$category", total: { $count: "" } } }
D. { $group: { _id: "$category", total: { $count: {} } } }

Solution

  1. Step 1: Recall $count syntax

    The $count accumulator does not take any arguments inside its braces; it should be empty: { $count: {} }.
  2. Step 2: Check each option

    Options A, B, and D incorrectly pass a field name or string to $count, which is invalid syntax.
  3. Final Answer:

    { $group: { _id: "$category", total: { $count: {} } } } -> Option D
  4. Quick Check:

    Empty braces for $count = correct syntax [OK]
Hint: Use empty braces {} inside $count always [OK]
Common Mistakes:
  • Passing a field name inside $count
  • Using empty string instead of empty braces
  • Confusing $count with $sum syntax
3. Given the collection documents:
{ "category": "A" }, { "category": "B" }, { "category": "A" }

What will be the output of this aggregation?
[ { $group: { _id: "$category", count: { $count: {} } } } ]
medium
A. SyntaxError: $count requires a field name
B. [ { "_id": "A", "count": 1 }, { "_id": "B", "count": 2 } ]
C. [ { "_id": "A", "count": 3 }, { "_id": "B", "count": 3 } ]
D. [ { "_id": "A", "count": 2 }, { "_id": "B", "count": 1 } ]

Solution

  1. Step 1: Understand $count usage

    The $count accumulator is not valid inside a $group stage. It is a separate pipeline stage used to count total documents, not per group.
  2. Step 2: Correct way to count documents per group

    To count documents per group, use $sum: 1 inside $group, not $count.
  3. Step 3: Result of using $count inside $group

    Using $count inside $group causes a syntax error.
  4. Final Answer:

    SyntaxError: $count requires a field name -> Option A
  5. Quick Check:

    $count is a pipeline stage, not an accumulator [OK]
Hint: Use $sum: 1 to count documents per group, not $count [OK]
Common Mistakes:
  • Using $count inside $group
  • Expecting $count to count per group
  • Confusing $count accumulator with $count stage
4. Identify the error in this aggregation pipeline:
[ { $group: { _id: "$type", total: { $count: "$amount" } } } ]
medium
A. Missing $match stage before $group
B. Incorrect _id field name
C. Using $count with a field argument is invalid
D. Using $count outside aggregation pipeline

Solution

  1. Step 1: Check $count usage

    The $count accumulator does not accept any arguments; passing "$amount" is invalid syntax.
  2. Step 2: Verify other parts

    The _id field and pipeline structure are correct; no $match is required before $group, and $count is inside aggregation.
  3. Final Answer:

    Using $count with a field argument is invalid -> Option C
  4. Quick Check:

    $count must have empty braces {} [OK]
Hint: Never pass a field to $count, use empty braces {} [OK]
Common Mistakes:
  • Passing a field name to $count
  • Thinking $count needs $match before $group
  • Confusing $count with $sum
5. You want to find how many orders each customer placed, but only for customers with more than 2 orders. Which aggregation pipeline correctly uses $count to achieve this?
hard
A. [ { $match: { orders: { $gt: 2 } } }, { $group: { _id: "$customer", orderCount: { $count: {} } } } ]
B. [ { $group: { _id: "$customer", orderCount: { $sum: 1 } } }, { $match: { orderCount: { $gt: 2 } } } ]
C. [ { $group: { _id: "$customer", orderCount: { $count: "$customer" } } }, { $match: { orderCount: { $gt: 2 } } } ]
D. [ { $group: { _id: "$customer", orderCount: { $count: {} } } }, { $match: { orderCount: { $gt: 2 } } } ]

Solution

  1. Step 1: Count orders per customer

    Since $count is not valid inside $group, use $sum: 1 to count documents per group.
  2. Step 2: Filter customers with more than 2 orders

    Use $match after grouping to filter customers with orderCount greater than 2.
  3. Final Answer:

    [ { $group: { _id: "$customer", orderCount: { $sum: 1 } } }, { $match: { orderCount: { $gt: 2 } } } ] -> Option B
  4. Quick Check:

    Use $sum:1 to count per group, then filter [OK]
Hint: Use $sum: 1 inside $group to count documents, then filter with $match [OK]
Common Mistakes:
  • Using $count inside $group
  • Passing a field to $count
  • Filtering before counting
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