Skip to content

Commit

Permalink
feat: add regex expression to raw query
Browse files Browse the repository at this point in the history
  • Loading branch information
diVid3 committed Feb 21, 2025
1 parent ccb70a1 commit 09feef9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/MongoQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/01. MonogQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}});

Expand Down

0 comments on commit 09feef9

Please sign in to comment.