@ -100,4 +100,13 @@ module.exports = function (db, module) {
callback(null, data.array);
});
};
module.listLength = function (key, callback) {
db.collection('objects').aggregate([
{ $match: { _key: key } },
{ $project: { count: { $size: "$array" } } },
], function (err, result) {
callback(err, Array.isArray(result) && result.length && result[0].count);
@ -56,4 +56,8 @@ module.exports = function (redisClient, module) {
}
redisClient.lrange(key, start, stop, callback);
redisClient.llen(key, callback);
@ -200,4 +200,19 @@ describe('List methods', function () {
it('should get the length of a list', function (done) {
db.listAppend('getLengthList', 1, function (err) {
assert.ifError(err);
db.listAppend('getLengthList', 2, function (err) {
db.listLength('getLengthList', function (err, length) {
assert.equal(length, 2);
done();