Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Data format
- // createdAt is broken down to 1 day (for example 2019-10-21T00:00:00.000+00:00)
- // Each key in the object represents an hour of that day (so key 16 represents 2019-10-21T16:00:00.000+00:00)
- [{
- product: 2815,
- region: 'us',
- createdAt: ISODate(),
- location: 'Berlin',
- details: {
- 16: { value: 2891, quantity: 2912, ... },
- 22: { value: 2891, quantity: 2912, ... }
- }
- }...]
- // Aggregation
- // Match returns about 600 documents and takes ~95% of the total time (4-6 secs)
- const data = await this.db.collection('...').aggregate([
- { $match: { product, createdAt: { $gte: new Date(Date.now() - sevenDaysAgo) }, region } },
- { $project: { createdAt: 1, details: { $objectToArray: '$details' } } },
- { $unwind: '$details' },
- {
- $group: {
- _id: '$scannedAt',
- value: { $avg: '$details.v.value' },
- quantity: { $avg: '$details.v.quantity' },
- ...
- }
- }
- ]).sort({ _id: 1 }).toArray()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement