From 437d2744ccf21e092311b920f20cdb9a7261ca24 Mon Sep 17 00:00:00 2001 From: yariplus Date: Sun, 16 Oct 2016 20:37:48 -0400 Subject: [PATCH] addl lex commands for mongo --- src/database/mongo/sorted.js | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 1a36f6eeb0..fa772666b3 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -579,7 +579,22 @@ module.exports = function (db, module) { }; module.getSortedSetRangeByLex = function (key, min, max, start, count, callback) { - var query = {_key: key}; + sortedSetLex(key, min, max, 1, start, count, callback); + }; + + module.getSortedSetRevRangeByLex = function (key, min, max, start, count, callback) { + sortedSetLex(key, min, max, -1, start, count, callback); + }; + + module.sortedSetLexCount = function (key, min, max, callback) { + sortedSetLex(key, min, max, 1, 0, 0, function (err, data) { + callback(err, data ? data.length : null); + }); + }; + + function sortedSetLex(method, sort, key, min, max, start, count, callback) { + var query = {_key: key, value: {}}; + if (min !== '-') { query.value = {$gte: min}; } @@ -587,8 +602,9 @@ module.exports = function (db, module) { query.value = query.value || {}; query.value.$lte = max; } + db.collection('objects').find(query, {_id: 0, value: 1}) - .sort({value: 1}) + .sort({value: sort}) .skip(start) .limit(count === -1 ? 0 : count) .toArray(function (err, data) { @@ -600,6 +616,24 @@ module.exports = function (db, module) { }); callback(err, data); }); + } + + module.sortedSetRemoveRangeByLex = function (key, min, max, callback) { + callback = callback || helpers.noop; + + var query = {_key: key}; + + if (min !== '-') { + query.value = {$gte: min}; + } + if (max !== '+') { + query.value = query.value || {}; + query.value.$lte = max; + } + + db.collection('objects').remove(query, function (err) { + callback(err); + }); }; module.processSortedSet = function (setKey, process, batch, callback) {