Bird
Raised Fist0
MongoDBquery~20 mins

$bucket and $bucketAuto for distribution in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bucket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Using $bucket to group ages into fixed ranges
Given a collection people with documents containing an age field, what is the output of this aggregation pipeline?

db.people.aggregate([
  {
    $bucket: {
      groupBy: "$age",
      boundaries: [0, 20, 40, 60],
      default: "Other",
      output: { count: { $sum: 1 } }
    }
  }
])
MongoDB
db.people.aggregate([
  {
    $bucket: {
      groupBy: "$age",
      boundaries: [0, 20, 40, 60],
      default: "Other",
      output: { count: { $sum: 1 } }
    }
  }
])
A[ { _id: 0, count: 3 }, { _id: 20, count: 5 }, { _id: 60, count: 2 }, { _id: "Other", count: 1 } ]
B[ { _id: 0, count: 3 }, { _id: 20, count: 5 }, { _id: 40, count: 2 } ]
C[ { _id: 0, count: 3 }, { _id: 20, count: 5 }, { _id: 40, count: 2 }, { _id: 60, count: 1 } ]
D[ { _id: 0, count: 3 }, { _id: 20, count: 5 }, { _id: 40, count: 2 }, { _id: "Other", count: 1 } ]
Attempts:
2 left
💡 Hint
Remember that $bucket groups values into the specified boundaries and uses the default bucket for values outside.
❓ query_result
intermediate
2:00remaining
Using $bucketAuto to create automatic buckets
Consider a collection sales with a price field. What does this aggregation output?

db.sales.aggregate([
  {
    $bucketAuto: {
      groupBy: "$price",
      buckets: 3,
      output: { count: { $sum: 1 } }
    }
  }
])
MongoDB
db.sales.aggregate([
  {
    $bucketAuto: {
      groupBy: "$price",
      buckets: 3,
      output: { count: { $sum: 1 } }
    }
  }
])
A[ { _id: { min: 10, max: 30 }, count: 4 }, { _id: { min: 31, max: 60 }, count: 3 }, { _id: { min: 61, max: 90 }, count: 3 } ]
B[ { _id: { min: 10, max: 40 }, count: 5 }, { _id: { min: 41, max: 70 }, count: 3 }, { _id: { min: 71, max: 90 }, count: 2 } ]
C[ { _id: { min: 10, max: 33 }, count: 3 }, { _id: { min: 34, max: 66 }, count: 4 }, { _id: { min: 67, max: 90 }, count: 3 } ]
D[ { _id: { min: 10, max: 25 }, count: 2 }, { _id: { min: 26, max: 55 }, count: 5 }, { _id: { min: 56, max: 90 }, count: 3 } ]
Attempts:
2 left
💡 Hint
The $bucketAuto stage divides data into roughly equal groups based on the data distribution.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in $bucket usage
Which option contains a syntax error in the $bucket stage?
MongoDB
db.collection.aggregate([
  {
    $bucket: {
      groupBy: "$score",
      boundaries: [0, 50, 100],
      output: { count: { $sum: 1 } }
    }
  }
])
A{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], output: { count: { $sum: 1 } } } }
B{ $bucket: { groupBy: "$score", boundaries: [0, 50], output: { count: { $sum: 1 } } } }
C{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], default: 0, output: { count: { $sum: 1 } } } }
D{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], default: "Other", output: { count: { $sum: 1 } } } }
Attempts:
2 left
💡 Hint
Check if the boundaries array has enough values for buckets.
❓ optimization
advanced
2:00remaining
Optimizing $bucketAuto for large datasets
Which option best improves performance when using $bucketAuto on a large collection with millions of documents?
AAdd an index on the field used in groupBy before running $bucketAuto.
BIncrease the number of buckets to the maximum allowed to reduce processing time.
CUse $bucket instead of $bucketAuto to avoid automatic boundary calculation.
DRun $bucketAuto without any indexes and limit the output to 10 documents.
Attempts:
2 left
💡 Hint
Indexes help queries and aggregations filter and group data faster.
🧠 Conceptual
expert
2:00remaining
Difference between $bucket and $bucketAuto
Which statement correctly describes a key difference between $bucket and $bucketAuto in MongoDB aggregation?
A$bucket requires predefined boundaries, while $bucketAuto calculates boundaries automatically to create roughly equal-sized buckets.
B$bucketAuto requires predefined boundaries, while $bucket calculates boundaries automatically based on data distribution.
CBoth $bucket and $bucketAuto require predefined boundaries but differ in output formatting.
D$bucket and $bucketAuto are identical in functionality but differ in syntax only.
Attempts:
2 left
💡 Hint
Think about how boundaries are set in each stage.

Practice

(1/5)
1. What is the main difference between $bucket and $bucketAuto in MongoDB aggregation?
easy
A. $bucket requires you to specify boundaries; $bucketAuto creates balanced buckets automatically.
B. $bucket automatically balances buckets; $bucketAuto requires manual boundaries.
C. $bucket groups by exact values; $bucketAuto groups by string length.
D. $bucket is used only for numeric data; $bucketAuto only for dates.

Solution

  1. Step 1: Understand $bucket behavior

    $bucket requires explicit boundaries to group documents into fixed ranges.
  2. Step 2: Understand $bucketAuto behavior

    $bucketAuto automatically calculates boundaries to create balanced buckets based on data distribution.
  3. Final Answer:

    $bucket requires you to specify boundaries; $bucketAuto creates balanced buckets automatically. -> Option A
  4. Quick Check:

    $bucket vs $bucketAuto = C [OK]
Hint: Remember: manual ranges for $bucket, auto ranges for $bucketAuto [OK]
Common Mistakes:
  • Confusing which operator auto-calculates boundaries
  • Thinking $bucketAuto needs manual boundaries
  • Assuming $bucket works only with strings
2. Which of the following is the correct syntax to use $bucket in a MongoDB aggregation pipeline?
easy
A. { $bucket: { groupBy: "$score", ranges: [0, 50, 100], default: "Other" } }
B. { $bucket: { field: "$score", ranges: [0, 50, 100] } }
C. { $bucketAuto: { groupBy: "$score", buckets: 3 } }
D. { $bucket: { groupBy: "$score", boundaries: [0, 50, 100] } }

Solution

  1. Step 1: Check required fields for $bucket

    $bucket requires groupBy and boundaries. The default field is optional.
  2. Step 2: Identify correct syntax

    { $bucket: { groupBy: "$score", boundaries: [0, 50, 100] } } correctly uses groupBy and boundaries without unnecessary fields.
  3. Final Answer:

    { $bucket: { groupBy: "$score", boundaries: [0, 50, 100] } } -> Option D
  4. Quick Check:

    Correct $bucket syntax = D [OK]
Hint: Remember: groupBy and boundaries are mandatory for $bucket [OK]
Common Mistakes:
  • Using field instead of groupBy
  • Confusing $bucket with $bucketAuto syntax
  • Omitting required boundaries array
3. Given the collection documents: [{score: 10}, {score: 20}, {score: 55}, {score: 70}, {score: 90}], what will be the output buckets using this aggregation?
{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], default: "Other", output: { count: { $sum: 1 } } } }
medium
A. [{_id: 0, count: 2}, {_id: 50, count: 2}, {_id: "Other", count: 1}]
B. [{_id: 0, count: 3}, {_id: 50, count: 2}]
C. [{_id: 0, count: 2}, {_id: 50, count: 3}]
D. [{_id: 0, count: 5}]

Solution

  1. Step 1: Assign documents to buckets by boundaries

    Boundaries are [0, 50, 100]. Scores 10, 20 fall into bucket 0-50 (count 2). Scores 55, 70, 90 fall into 50-100 (count 3).
  2. Step 2: Check output format

    Output counts per bucket: {_id: 0, count: 2} and {_id: 50, count: 3}.
  3. Final Answer:

    [{_id: 0, count: 2}, {_id: 50, count: 3}] -> Option C
  4. Quick Check:

    Bucket counts match boundaries = A [OK]
Hint: Count docs per boundary range carefully [OK]
Common Mistakes:
  • Mixing counts between buckets
  • Misunderstanding boundaries as inclusive/exclusive
  • Assuming default bucket is used when not needed
4. You wrote this aggregation but get an error:
{ $bucket: { groupBy: "$age", boundaries: [20, 30, 40], default: "Other" } }
The error says: "Boundaries must be sorted in ascending order and unique." What is the likely cause?
medium
A. Boundaries array is missing the lowest value 0.
B. Boundaries array is not sorted or has duplicates.
C. The default value must be a number, not a string.
D. The groupBy field must be an integer, not a string.

Solution

  1. Step 1: Understand error message

    The error says boundaries must be sorted ascending and unique, so check boundaries array.
  2. Step 2: Check boundaries array

    If boundaries are not sorted or have duplicates, error occurs. The example boundaries [20, 30, 40] are sorted and unique, so likely in actual code they are not.
  3. Final Answer:

    Boundaries array is not sorted or has duplicates. -> Option B
  4. Quick Check:

    Boundaries must be sorted unique = B [OK]
Hint: Check boundaries array order and duplicates first [OK]
Common Mistakes:
  • Ignoring error message details
  • Thinking default value type causes this error
  • Assuming groupBy field type causes this error
5. You want to create 4 buckets automatically for a collection with a wide range of numeric values in field price. Which aggregation stage correctly achieves this using $bucketAuto?
hard
A. { $bucketAuto: { groupBy: "$price", buckets: 4, output: { count: { $sum: 1 }, avgPrice: { $avg: "$price" } } } }
B. { $bucket: { groupBy: "$price", boundaries: [0, 25, 50, 75, 100] } }
C. { $bucketAuto: { groupBy: "$price", boundaries: [0, 25, 50, 75, 100] } }
D. { $bucketAuto: { groupBy: "$price", buckets: "4" } }

Solution

  1. Step 1: Identify correct $bucketAuto usage

    $bucketAuto requires groupBy and buckets (number). { $bucketAuto: { groupBy: "$price", buckets: 4, output: { count: { $sum: 1 }, avgPrice: { $avg: "$price" } } } } uses these correctly and includes output aggregation.
  2. Step 2: Eliminate incorrect options

    { $bucket: { groupBy: "$price", boundaries: [0, 25, 50, 75, 100] } } uses $bucket with manual boundaries, not automatic. { $bucketAuto: { groupBy: "$price", boundaries: [0, 25, 50, 75, 100] } } incorrectly uses boundaries with $bucketAuto. { $bucketAuto: { groupBy: "$price", buckets: "4" } } uses string for buckets count, which must be a number.
  3. Final Answer:

    { $bucketAuto: { groupBy: "$price", buckets: 4, output: { count: { $sum: 1 }, avgPrice: { $avg: "$price" } } } } -> Option A
  4. Quick Check:

    Correct $bucketAuto syntax with numeric buckets = A [OK]
Hint: Use numeric buckets count and no boundaries with $bucketAuto [OK]
Common Mistakes:
  • Using boundaries with $bucketAuto
  • Passing buckets as string instead of number
  • Confusing $bucket and $bucketAuto syntax
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