From 988afab81c0de4fbc4f6d8c46b64c1d45f1a90cf Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 27 Sep 2023 15:18:56 +0200 Subject: [PATCH] fix most listened tracks. fix #19 --- services/database/tracks.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/services/database/tracks.js b/services/database/tracks.js index afad715..87d35c3 100644 --- a/services/database/tracks.js +++ b/services/database/tracks.js @@ -97,17 +97,27 @@ exports.mostListened = function (filter, callback) { } }, { $match: { "album.visibility": { $in: filter } } }); } else { - aggregate.unshift({ $match: { type: 'track' } }); - } - aggregate.push({ $sort: { counter: -1, _id: -1 } }, { $limit: 6 }) + dbo.collection("history") + .find() + .project({ _id: true }) + .sort({ _id: -1 }) + .limit(1000) + .toArray((err, ids) => { + let arr = ids.map(h => h._id); + + aggregate.unshift({ $match: { type: 'track', _id: { $in: arr } } }); + aggregate.push({ $sort: { counter: -1 } }, { $limit: 6 }) + + dbo + .collection("history") + .aggregate(aggregate, { + allowDiskUse: true + }) + .toArray((err, result) => { + if (err) throw err; + callback(result); + }); + }); + } - dbo - .collection("history") - .aggregate(aggregate, { - allowDiskUse: true - }) - .toArray((err, result) => { - if (err) throw err; - callback(result); - }); }; \ No newline at end of file