@@ -31,17 +31,17 @@ QueryBuilder.defaults({
31
31
} ,
32
32
33
33
mongoRuleOperators : {
34
- $ne : function ( v ) {
35
- v = v . $ne ;
34
+ $eq : function ( v ) {
36
35
return {
37
36
'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 ' )
39
38
} ;
40
39
} ,
41
- eq : function ( v ) {
40
+ $ne : function ( v ) {
41
+ v = v . $ne ;
42
42
return {
43
43
'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 ' )
45
45
} ;
46
46
} ,
47
47
$regex : function ( v ) {
@@ -224,7 +224,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
224
224
} ;
225
225
}
226
226
227
- var key = andOr ( query ) ;
227
+ var key = self . getMongoCondition ( query ) ;
228
228
if ( ! key ) {
229
229
Utils . error ( 'MongoParse' , 'Invalid MongoDB query format' ) ;
230
230
}
@@ -249,15 +249,15 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
249
249
return ;
250
250
}
251
251
252
- var key = andOr ( data ) ;
252
+ var key = self . getMongoCondition ( data ) ;
253
253
if ( key ) {
254
254
parts . push ( parse ( data , key ) ) ;
255
255
}
256
256
else {
257
257
var field = Object . keys ( data ) [ 0 ] ;
258
258
var value = data [ field ] ;
259
259
260
- var operator = determineMongoOperator ( value , field ) ;
260
+ var operator = self . getMongoOperator ( value ) ;
261
261
if ( operator === undefined ) {
262
262
Utils . error ( 'MongoParse' , 'Invalid MongoDB query format' ) ;
263
263
}
@@ -344,58 +344,50 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
344
344
}
345
345
346
346
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
+ } ,
360
348
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 ) {
366
358
return 'between' ;
367
359
}
368
- if ( value . $lt !== undefined && value . $gt !== undefined ) {
360
+ if ( data . $lt !== undefined && data . $gt !== undefined ) {
369
361
return 'not_between' ;
370
362
}
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 ] ;
376
370
}
377
371
}
378
- }
379
- else {
380
- return 'eq' ;
381
- }
382
- }
372
+ else {
373
+ return '$eq' ;
374
+ }
375
+ } ,
383
376
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 ) ;
393
377
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
+ }
397
391
}
398
392
}
399
-
400
- return undefined ;
401
- }
393
+ } ) ;
0 commit comments