Skip to content

Commit 0d41d9c

Browse files
committed
Fix #681 : Loose MongoDB operator detection (allow additional operators)
1 parent 7699723 commit 0d41d9c

File tree

1 file changed

+44
-52
lines changed

1 file changed

+44
-52
lines changed

src/plugins/mongodb-support/plugin.js

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ QueryBuilder.defaults({
3131
},
3232

3333
mongoRuleOperators: {
34-
$ne: function(v) {
35-
v = v.$ne;
34+
$eq: function(v) {
3635
return {
3736
'val': v,
38-
'op': v === null ? 'is_not_null' : (v === '' ? 'is_not_empty' : 'not_equal')
37+
'op': v === null ? 'is_null' : (v === '' ? 'is_empty' : 'equal')
3938
};
4039
},
41-
eq: function(v) {
40+
$ne: function(v) {
41+
v = v.$ne;
4242
return {
4343
'val': v,
44-
'op': v === null ? 'is_null' : (v === '' ? 'is_empty' : 'equal')
44+
'op': v === null ? 'is_not_null' : (v === '' ? 'is_not_empty' : 'not_equal')
4545
};
4646
},
4747
$regex: function(v) {
@@ -224,7 +224,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
224224
};
225225
}
226226

227-
var key = andOr(query);
227+
var key = self.getMongoCondition(query);
228228
if (!key) {
229229
Utils.error('MongoParse', 'Invalid MongoDB query format');
230230
}
@@ -249,15 +249,15 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
249249
return;
250250
}
251251

252-
var key = andOr(data);
252+
var key = self.getMongoCondition(data);
253253
if (key) {
254254
parts.push(parse(data, key));
255255
}
256256
else {
257257
var field = Object.keys(data)[0];
258258
var value = data[field];
259259

260-
var operator = determineMongoOperator(value, field);
260+
var operator = self.getMongoOperator(value);
261261
if (operator === undefined) {
262262
Utils.error('MongoParse', 'Invalid MongoDB query format');
263263
}
@@ -344,58 +344,50 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
344344
}
345345

346346
return id;
347-
}
348-
});
349-
350-
/**
351-
* Finds which operator is used in a MongoDB sub-object
352-
* @memberof module:plugins.MongoDbSupport
353-
* @param {*} value
354-
* @returns {string|undefined}
355-
* @private
356-
*/
357-
function determineMongoOperator(value) {
358-
if (value !== null && typeof value == 'object') {
359-
var subkeys = Object.keys(value);
347+
},
360348

361-
if (subkeys.length === 1) {
362-
return subkeys[0];
363-
}
364-
else {
365-
if (value.$gte !== undefined && value.$lte !== undefined) {
349+
/**
350+
* Finds which operator is used in a MongoDB sub-object
351+
* @param {*} data
352+
* @returns {string|undefined}
353+
* @private
354+
*/
355+
getMongoOperator: function(data) {
356+
if (data !== null && typeof data === 'object') {
357+
if (data.$gte !== undefined && data.$lte !== undefined) {
366358
return 'between';
367359
}
368-
if (value.$lt !== undefined && value.$gt !== undefined) {
360+
if (data.$lt !== undefined && data.$gt !== undefined) {
369361
return 'not_between';
370362
}
371-
else if (value.$regex !== undefined) { // optional $options
372-
return '$regex';
373-
}
374-
else {
375-
return;
363+
364+
var knownKeys = Object.keys(data).filter(function(key) {
365+
return !!this.settings.mongoRuleOperators[key];
366+
}.bind(this));
367+
368+
if (knownKeys.length === 1) {
369+
return knownKeys[0];
376370
}
377371
}
378-
}
379-
else {
380-
return 'eq';
381-
}
382-
}
372+
else {
373+
return '$eq';
374+
}
375+
},
383376

384-
/**
385-
* Returns the key corresponding to "$or" or "$and"
386-
* @memberof module:plugins.MongoDbSupport
387-
* @param {object} data
388-
* @returns {string}
389-
* @private
390-
*/
391-
function andOr(data) {
392-
var keys = Object.keys(data);
393377

394-
for (var i = 0, l = keys.length; i < l; i++) {
395-
if (keys[i].toLowerCase() == '$or' || keys[i].toLowerCase() == '$and') {
396-
return keys[i];
378+
/**
379+
* Returns the key corresponding to "$or" or "$and"
380+
* @param {object} data
381+
* @returns {string|undefined}
382+
* @private
383+
*/
384+
getMongoCondition: function(data) {
385+
var keys = Object.keys(data);
386+
387+
for (var i = 0, l = keys.length; i < l; i++) {
388+
if (keys[i].toLowerCase() === '$or' || keys[i].toLowerCase() === '$and') {
389+
return keys[i];
390+
}
397391
}
398392
}
399-
400-
return undefined;
401-
}
393+
});

0 commit comments

Comments
 (0)