diff --git a/lib/handlers/on-search.js b/lib/handlers/on-search.js index 0fa25e8d..7ac1a76a 100644 --- a/lib/handlers/on-search.js +++ b/lib/handlers/on-search.js @@ -1,6 +1,7 @@ 'use strict'; const db = require('../db'); +const tools = require('../tools'); /** * Returns an array of matching UID values @@ -93,9 +94,7 @@ module.exports = server => (path, options, session, callback) => { if (term.value.length !== session.selected.uidList.length) { // not 1:* parent.push({ - uid: { - [!ne ? '$in' : '$nin']: term.value - } + uid: tools.checkRangeQuery(term.value, ne) }); } else if (ne) { parent.push({ diff --git a/lib/tools.js b/lib/tools.js index 650dad7d..a8884d4b 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -2,28 +2,37 @@ const punycode = require('punycode'); -function checkRangeQuery(uids) { +function checkRangeQuery(uids, ne) { // check if uids is a straight continous array and if such then return a range query, // otherwise retrun a $in query if (uids.length === 1) { return { - $eq: uids[0] + [!ne ? '$eq' : '$ne']: uids[0] }; } for (let i = 1, len = uids.length; i < len; i++) { if (uids[i] !== uids[i - 1] + 1) { return { - $in: uids + [!ne ? '$in' : '$nin']: uids }; } } - return { - $gte: uids[0], - $lte: uids[uids.length - 1] - }; + if (!ne) { + return { + $gte: uids[0], + $lte: uids[uids.length - 1] + }; + } else { + return { + $not: { + $gte: uids[0], + $lte: uids[uids.length - 1] + } + }; + } } function normalizeAddress(address, withNames) {