sortedSetUnionCard

v1.18.x
barisusakli 9 years ago
parent 77c549081c
commit e1263f1057

@ -474,6 +474,26 @@ module.exports = function(db, module) {
});
};
module.sortedSetUnionCard = function(keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, 0);
}
var pipeline = [
{ $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, function(err, data) {
console.log(data);
callback(err, Array.isArray(data) && data.length ? data[0].count : 0);
});
};
module.getSortedSetUnion = function(params, callback) {
params.sort = 1;
getSortedSetUnion(params, callback);
@ -484,7 +504,6 @@ module.exports = function(db, module) {
getSortedSetUnion(params, callback);
};
function getSortedSetUnion(params, callback) {
if (!Array.isArray(params.sets) || !params.sets.length) {
return callback();

@ -232,6 +232,23 @@ module.exports = function(redisClient, module) {
multi.exec(callback);
};
module.sortedSetUnionCard = function(keys, callback) {
var tempSetName = 'temp_' + Date.now();
var multi = redisClient.multi();
multi.zunionstore([tempSetName, keys.length].concat(keys));
multi.zcard(tempSetName);
multi.del(tempSetName);
multi.exec(function(err, results) {
if (err) {
return callback(err);
}
callback(null, Array.isArray(results) && results.length ? results[1] : 0);
});
};
module.getSortedSetUnion = function(params, callback) {
params.method = 'zrange';
sortedSetUnion(params, callback);

@ -436,6 +436,16 @@ describe('Sorted Set methods', function() {
});
});
describe('sortedSetUnionCard', function() {
it('should return the number of elements in the union', function(done) {
db.sortedSetUnionCard(['sortedSetTest2', 'sortedSetTest3'], function(err, count) {
assert.ifError(err);
assert.equal(count, 3);
done();
});
});
});
describe('getSortedSetUnion()', function() {
it('should return an array of values from both sorted sets sorted by scores lowest to highest', function(done) {
db.getSortedSetUnion({sets: ['sortedSetTest2', 'sortedSetTest3'], start: 0, stop: -1}, function(err, values) {

Loading…
Cancel
Save