const mongoose = require('mongoose'); mongoose.connect('mongodb://backone_inspect:backone_inspect@mongodb.prod.proit.id:27017/backone_inspect_0') .then(async (m) => { const pipeline = [ { $group: { _id: '$app_label', download: { $sum: '$download' }, upload: { $sum: '$upload' }, flows: { $sum: '$flows' }, }}, { $sort: { download: -1 } }, { $limit: 3 } ]; let result = await m.connection.db.collection('appstats').aggregate(pipeline).toArray(); if (result.length === 0) { console.log('Falling back to flows...'); result = await m.connection.db.collection('flows').aggregate([ { $match: { app_label: { $ne: null, $ne: '' } } }, { $group: { _id: '$app_label', download: { $sum: '$download' }, upload: { $sum: '$upload' }, flows: { $sum: 1 }, }}, { $sort: { download: -1 } }, { $limit: 3 } ]).toArray(); } console.log(result); process.exit(0); });