0
0
MongoDBquery~10 mins

Bucket pattern for time-series data in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a bucket field that groups timestamps by hour.

MongoDB
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }])
Drag options to blanks, or click blank then click option'
Ahour
Bminute
Cday
Dsecond
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'minute' or 'second' will create too many small buckets.
Using 'day' groups data by whole days, not hours.
2fill in blank
medium

Complete the code to group documents by the bucket field and count entries per bucket.

MongoDB
db.sensorData.aggregate([{ $group: { _id: "$[1]", count: { $sum: 1 } } }])
Drag options to blanks, or click blank then click option'
Atimestamp
BsensorId
Cbucket
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by 'timestamp' groups by exact time, not buckets.
Grouping by 'sensorId' groups by sensor, not time.
3fill in blank
hard

Fix the error in the pipeline stage that filters documents within a specific bucket range.

MongoDB
db.sensorData.aggregate([{ $match: { bucket: { $gte: ISODate("2023-01-01T00:00:00Z"), [1]: ISODate("2023-01-01T23:59:59Z") } } }])
Drag options to blanks, or click blank then click option'
A$lte
B$lt
C$eq
D$ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt excludes documents exactly at the upper bound.
Using $eq or $ne does not specify a range.
4fill in blank
hard

Fill both blanks to create a bucket pattern that groups data by day and calculates average value.

MongoDB
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }, { $group: { _id: "$bucket", avgValue: { $[2]: "$value" } } }])
Drag options to blanks, or click blank then click option'
Aday
Bhour
Cavg
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hour' groups by hour, not day.
Using 'sum' adds values instead of averaging.
5fill in blank
hard

Fill all three blanks to create a bucket pattern that groups data by month, counts entries, and filters buckets with more than 100 entries.

MongoDB
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }, { $group: { _id: "$bucket", count: { $[2]: 1 } } }, { $match: { count: { $[3]: 100 } } }])
Drag options to blanks, or click blank then click option'
Amonth
Bsum
C$gt
Dday
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' instead of 'month' groups by day.
Using '$lt' instead of '$gt' filters wrong buckets.
Using 'avg' instead of 'sum' does not count entries.