Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var StartTime = (Date.now() - (60*60*24*1000)) / 1000;
- var EndTime = (Date.now() / 1000);
- var Interval = (60*30); // half an hour (this should be dynamic as much as possible)
- var SensorType = "acc";
- // This query takes 20 seconds (And i have 5 million records in my collection)
- SensorData.aggregate([
- {
- $match: {
- date: {
- $gte: StartTime * 1000,
- $lt: EndTime * 1000
- },
- SensorType: SensorType,
- }
- },
- // Group by Interval
- {
- $group: {
- _id: Object.assign({
- timestamp: {
- $subtract: [
- {
- $divide: [ '$date', Interval * 1000]
- },
- {
- $mod: [
- {
- $divide: ['$date', Interval * 1000]
- }, 1]
- }]
- }
- }),
- value: { $avg: "$Movement" }
- }
- },
- // Sort by date
- {
- $sort: { "_id.timestamp": 1 }
- },
- // Format date & timestamp
- {
- $project: {
- _id: 0,
- timestamp: {
- $multiply: [Interval * 1000, "$_id.timestamp"]
- },
- value: "$value",
- date: {
- $add: [new Date(0), {
- $multiply: [Interval * 1000, "$_id.timestamp"],
- }],
- }
- }
- }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement