Skip to content

Instantly share code, notes, and snippets.

@izquiratops
Last active June 26, 2020 08:01
Show Gist options
  • Save izquiratops/b0504dc562debe4acd71b23c46c8a19e to your computer and use it in GitHub Desktop.
Save izquiratops/b0504dc562debe4acd71b23c46c8a19e to your computer and use it in GitHub Desktop.
use('globalDrone-Main');
const aggregationTotalBytes = [
{ $match: {
deviceId: '78ea0d4d-876e-4543-bfbb-4a00789e9cfd',
time: { $gte: new Date('2019-01-01'), $lt: new Date('2020-06-19') }
} },
{ $group: {
_id: {
month: { $month: '$_id' },
year: { $year: '$_id' }
},
totalBytesAPReceived: { $sum: '$totalBytesAPReceived' },
totalBytesAPSent: { $sum: '$totalBytesAPSent' },
totalBytesLatencyReceived: { $sum: '$totalBytesLatencyReceived' },
totalBytesLatencySent: { $sum: '$totalBytesLatencySent' },
totalBytesReceived: { $sum: '$totalBytesReceived' },
totalBytesSent: { $sum: '$totalBytesSent' },
totalBytesSWSent: { $sum: '$totalBytesSWSent' }
} },
{ $project: {
_id: '$_id',
totalBytesAPReceived: '$totalBytesAPReceived',
totalBytesAPSent: '$totalBytesAPSent',
totalBytesLatencyReceived: '$totalBytesLatencyReceived',
totalBytesLatencySent: '$totalBytesLatencySent',
totalBytesReceived: '$totalBytesReceived',
totalBytesSent: '$totalBytesSent',
totalBytesSWSent: '$totalBytesSWSent',
} }
];
const aggregationOperations = [
{ $match: {
deviceId: '78ea0d4d-876e-4543-bfbb-4a00789e9cfd',
time: { $gte: new Date('2019-01-01'), $lt: new Date('2020-06-19') }
} },
{ $group: {
_id: "$_id",
flightOperationId: { $addToSet: '$flightOperationId' }
} },
{ $unwind: "$flightOperationId" },
{ $group: {
_id: {
month: { $month: '$_id' },
year: { $year: '$_id' }
},
count: { $sum: 1 }
} },
{ $project: {
_id: '$_id',
count: '$count',
} }
];
const aggregationFacet = [
{ $match: {
deviceId: '78ea0d4d-876e-4543-bfbb-4a00789e9cfd',
time: { $gte: new Date('2019-01-01'), $lt: new Date('2020-06-19') }
} },
{ $facet: {
totalBytesGroup: [
{ $group: {
_id: {
month: { $month: '$_id' },
year: { $year: '$_id' }
},
totalBytesAPReceived: { $sum: '$totalBytesAPReceived' },
totalBytesAPSent: { $sum: '$totalBytesAPSent' },
totalBytesLatencyReceived: { $sum: '$totalBytesLatencyReceived' },
totalBytesLatencySent: { $sum: '$totalBytesLatencySent' },
totalBytesReceived: { $sum: '$totalBytesReceived' },
totalBytesSent: { $sum: '$totalBytesSent' },
totalBytesSWSent: { $sum: '$totalBytesSWSent' }
} }
],
totalOperationsGroup: [
{ $group: {
_id: "$_id",
flightOperationId: { $addToSet: '$flightOperationId' }
} },
{ $unwind: "$flightOperationId" },
{ $group: {
_id: {
month: { $month: '$_id' },
year: { $year: '$_id' }
},
totalOperations: { $sum: 1 }
} }
]
} },
{ $project: {
data: {
$map: {
input: { $zip: { inputs: ["$totalOperationsGroup", "$totalBytesGroup"], useLongestLength: true } },
as: "item",
in: {
$reduce: {
input: "$$item",
initialValue: {},
in: { "$mergeObjects": ["$$this","$$value"]}
}
}
}
}
} },
// { $project: { mergedData: { "$setUnion": ["$totalOperationsGroup", "$totalBytesGroup"] } } },
{ $unwind: "$data" },
{ $replaceRoot: { "newRoot": "$data" } }
]
db.dataAccount.aggregate(aggregationFacet);
// db.dataAccount.find({ time: { $gte: new Date('2019-01-01'), $lt: new Date('2020-08-01') } });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment