Complete the code to create a bucket field that groups timestamps by hour.
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }])The $dateTrunc operator truncates the timestamp to the specified unit. Using "hour" groups data into hourly buckets.
Complete the code to group documents by the bucket field and count entries per bucket.
db.sensorData.aggregate([{ $group: { _id: "$[1]", count: { $sum: 1 } } }])Grouping by the bucket field groups documents into time buckets, allowing counting entries per bucket.
Fix the error in the pipeline stage that filters documents within a specific bucket range.
db.sensorData.aggregate([{ $match: { bucket: { $gte: ISODate("2023-01-01T00:00:00Z"), [1]: ISODate("2023-01-01T23:59:59Z") } } }])To filter documents between two dates inclusive, use $gte and $lte operators.
Fill both blanks to create a bucket pattern that groups data by day and calculates average value.
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }, { $group: { _id: "$bucket", avgValue: { $[2]: "$value" } } }])Grouping by day buckets groups data daily. Using avg calculates the average value per day.
Fill all three blanks to create a bucket pattern that groups data by month, counts entries, and filters buckets with more than 100 entries.
db.sensorData.aggregate([{ $addFields: { bucket: { $dateTrunc: { date: "$timestamp", unit: [1] } } } }, { $group: { _id: "$bucket", count: { $[2]: 1 } } }, { $match: { count: { $[3]: 100 } } }])Grouping by month buckets groups data monthly. Using sum counts entries. Filtering with $gt selects buckets with more than 100 entries.