feat: shorter setsCard

v1.18.x
Barış Soner Uşaklı 6 years ago
parent b3ed89f5fd
commit 3780a58ffb

@ -159,32 +159,7 @@ module.exports = function (db, module) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, []);
}
var pipeline = [
{ $match: { _key: { $in: keys } } },
{ $group: { _id: { _key: '$_key' }, count: { $sum: 1 } } },
{ $project: { _id: 1, count: '$count' } },
];
db.collection('objects').aggregate(pipeline).toArray(function (err, results) {
if (err) {
return callback(err);
}
if (!Array.isArray(results)) {
results = [];
}
var map = {};
results.forEach(function (item) {
if (item && item._id._key) {
map[item._id._key] = item.count;
}
});
results = keys.map(function (key) {
return map[key] || 0;
});
callback(null, results);
});
async.map(keys, module.sortedSetCard, callback);
};
module.sortedSetRank = function (key, value, callback) {

@ -6,16 +6,12 @@ module.exports = function (db, module) {
return callback(null, 0);
}
var pipeline = [
db.collection('objects').aggregate([
{ $match: { _key: { $in: keys } } },
{ $group: { _id: { value: '$value' } } },
{ $group: { _id: null, count: { $sum: 1 } } },
];
var project = { _id: 0, count: '$count' };
pipeline.push({ $project: project });
db.collection('objects').aggregate(pipeline).toArray(function (err, data) {
{ $project: { _id: 0, count: '$count' } },
]).toArray(function (err, data) {
callback(err, Array.isArray(data) && data.length ? data[0].count : 0);
});
};

Loading…
Cancel
Save