Courseiva
Aggregation Framework →hardMultiple Choice

C100DEV Aggregation Framework Practice Question

A financial application stores transactions in a collection named transactions. Each document has fields: `accountId` (string), `amount` (decimal), `type` (string, either 'debit' or 'credit'), and `timestamp` (date). The team needs to calculate the net balance per account, defined as the sum of credits minus the sum of debits. The result should include only accounts with at least 10 transactions. Which aggregation pipeline achieves this?

⚠ Common exam trap

The trap here is using $gt instead of $gte when the requirement says 'at least 10', which includes exactly 10.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

db.transactions.aggregate([ { $group: { _id: "$accountId", netBalance: { $sum: { $cond: [ { $eq: [ "$type", "credit" ] }, "$amount", { $multiply: [ "$amount", -1 ] } ] } }, count: { $sum: 1 } } }, { $match: { count: { $gte: 10 } } } ])

The correct pipeline groups transactions by accountId, computes net balance by conditionally adding credits and subtracting debits, and counts transactions. It then filters groups with a count of at least 10 using $match. This approach correctly handles the arithmetic and the threshold, and it is efficient because grouping is necessary to compute the count per account.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    db.transactions.aggregate([ { $group: { _id: "$accountId", netBalance: { $sum: { $cond: [ { $eq: [ "$type", "credit" ] }, "$amount", { $multiply: [ "$amount", -1 ] } ] } }, count: { $sum: 1 } } }, { $match: { count: { $gte: 10 } } } ])

    Why this is correct

    This pipeline groups by accountId, using $cond to add amount for credits and subtract amount for debits, effectively computing net balance. It also counts transactions per account. Then it filters groups with count >= 10 using $match. This correctly implements the requirement and is efficient because the grouping is done first, then filtered by count, which is necessary since the count is computed during grouping.

  • ✗

    db.transactions.aggregate([ { $match: { $expr: { $gte: [ { $size: "$transactions" }, 10 ] } } }, { $group: { _id: "$accountId", netBalance: { $sum: { $cond: [ { $eq: [ "$type", "credit" ] }, "$amount", { $multiply: [ "$amount", -1 ] } ] } } } } ])

    Why it's wrong here

    The $match stage uses $expr with $size on a field `transactions`, which does not exist in the documents. The documents have individual transaction fields, not an array of transactions. This will result in an error or no matches. The correct approach is to count transactions per account during grouping, not to reference a non-existent array.

  • ✗

    db.transactions.aggregate([ { $group: { _id: "$accountId", netBalance: { $sum: { $cond: [ { $eq: [ "$type", "credit" ] }, "$amount", { $multiply: [ "$amount", -1 ] } ] } }, count: { $sum: 1 } } }, { $match: { count: { $gt: 10 } } } ])

    Why it's wrong here

    This pipeline is almost correct but uses `$gt: 10` instead of `$gte: 10`. The requirement is at least 10 transactions, which includes exactly 10. Using $gt excludes accounts with exactly 10 transactions. The correct operator is $gte. Everything else is correct, but this subtle difference makes the result incorrect.

  • ✗

    db.transactions.aggregate([ { $group: { _id: "$accountId", netBalance: { $sum: { $cond: [ { $eq: [ "$type", "credit" ] }, "$amount", { $multiply: [ "$amount", -1 ] } ] } }, count: { $sum: 1 } } }, { $having: { count: { $gte: 10 } } } ])

    Why it's wrong here

    The pipeline uses a $having stage, which does not exist in MongoDB aggregation framework. MongoDB uses $match to filter after grouping. Therefore, this pipeline will fail with an unrecognized stage error. The correct stage to filter grouped results is $match, and it should be placed after $group.

About these practice questions

One of 259 original C100DEV practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official MongoDB exam blueprint

This C100DEV practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DEV exam.