0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.