diff --git a/lib/MongoQuery.js b/lib/MongoQuery.js index 2314186..ed643db 100644 --- a/lib/MongoQuery.js +++ b/lib/MongoQuery.js @@ -82,7 +82,7 @@ class MongoQuery { for (const key in obj) { if (!obj.hasOwnProperty(key)) continue; - if ($check.object(obj[key]) && !utils.isValidId(obj[key])) { + if ($check.object(obj[key]) && !utils.isValidId(obj[key]) && key !== '$regularExpression') { mergeRawQueryRecursive(obj[key], obj, key); } else if ($check.array(obj[key])) { for (let i = 0; i < obj[key].length; i++) { @@ -114,6 +114,8 @@ class MongoQuery { parent[parentKey] = queryVal; } else if (key === '$string') { parent[parentKey] = $check.assigned(obj[key]) && obj[key].toString ? obj[key].toString() : obj[key]; + } else if (key === '$regularExpression') { + parent[parentKey] = new RegExp(obj[key].pattern, obj[key].options); } } } diff --git a/package.json b/package.json index 297c1bb..2bbfd9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@synatic/mongo-magic", - "version": "2.3.2", + "version": "2.3.3", "description": "Synatic utility classes for interacting with MongoDB", "main": "index.js", "files": [ diff --git a/test/01. MonogQuery.js b/test/01. MonogQuery.js index 9f82c80..3814687 100644 --- a/test/01. MonogQuery.js +++ b/test/01. MonogQuery.js @@ -147,6 +147,19 @@ describe('Mongo Query', function () { done(); }); + it('should parse a raw query with regularExpression in \'$in\' array', function (done) { + const mongoQuery = new MongoQuery({$rawQuery: { + field: {$in: [ + {$regularExpression: { pattern: ';', options: 'i' }}, + {$regularExpression: { pattern: '/', options: 'i' }} + ]}} + }); + + assert(mongoQuery.parsedQuery.query.field.$in[0] instanceof RegExp, 'Invalid parsed $in RegExp query'); + assert(mongoQuery.parsedQuery.query.field.$in[1] instanceof RegExp, 'Invalid parsed $in RegExp query'); + done(); + }); + it('should parse a raw query with int', function (done) { const mongoQuery = new MongoQuery({$rawQuery: {'field1.field2': {$float: '1.2'}}});