diff --git a/src/database/mongo/list.js b/src/database/mongo/list.js index 0c5e2955e5..fb800eb696 100644 --- a/src/database/mongo/list.js +++ b/src/database/mongo/list.js @@ -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); + }); + }; }; diff --git a/src/database/redis/list.js b/src/database/redis/list.js index f8108a194d..ba127d3100 100644 --- a/src/database/redis/list.js +++ b/src/database/redis/list.js @@ -56,4 +56,8 @@ module.exports = function (redisClient, module) { } redisClient.lrange(key, start, stop, callback); }; + + module.listLength = function (key, callback) { + redisClient.llen(key, callback); + }; }; diff --git a/test/database/list.js b/test/database/list.js index 063a316b2d..23768eb85d 100644 --- a/test/database/list.js +++ b/test/database/list.js @@ -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) { + assert.ifError(err); + db.listLength('getLengthList', function (err, length) { + assert.ifError(err); + assert.equal(length, 2); + done(); + }); + }); + }); + }); });