Skip to content

Commit

Permalink
Merge pull request #86 from arimus/master
Browse files Browse the repository at this point in the history
Support for regex flags in like/nlike operator
  • Loading branch information
raymondfeng committed Feb 4, 2015
2 parents aba1119 + 6b6b154 commit c6df057
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ MongoDB.prototype.buildWhere = function (model, where) {
return;
}
var spec = false;
var options = null;
if (cond && cond.constructor.name === 'Object') {
options = cond.options;
spec = Object.keys(cond)[0];
cond = cond[spec];
}
Expand All @@ -476,9 +478,9 @@ MongoDB.prototype.buildWhere = function (model, where) {
return ObjectID(x);
})};
} else if (spec === 'like') {
query[k] = {$regex: new RegExp(cond)};
query[k] = {$regex: new RegExp(cond, options)};
} else if (spec === 'nlike') {
query[k] = {$not: new RegExp(cond)};
query[k] = {$not: new RegExp(cond, options)};
} else if (spec === 'neq') {
query[k] = {$ne: cond};
}
Expand Down
20 changes: 20 additions & 0 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,16 @@ describe('mongodb', function () {
});
});

it('should allow to find using case insensitive like', function (done) {
Post.create({title: 'My Post', content: 'Hello'}, function (err, post) {
Post.find({where: {title: {like: 'm.+st', options: 'i'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});

it('should support like for no match', function (done) {
Post.create({title: 'My Post', content: 'Hello'}, function (err, post) {
Post.find({where: {title: {like: 'M.+XY'}}}, function (err, posts) {
Expand All @@ -1202,6 +1212,16 @@ describe('mongodb', function () {
});
});

it('should allow to find using case insensitive nlike', function (done) {
Post.create({title: 'My Post', content: 'Hello'}, function (err, post) {
Post.find({where: {title: {nlike: 'm.+st', options: 'i'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});

it('should support nlike for no match', function (done) {
Post.create({title: 'My Post', content: 'Hello'}, function (err, post) {
Post.find({where: {title: {nlike: 'M.+XY'}}}, function (err, posts) {
Expand Down

0 comments on commit c6df057

Please sign in to comment.