From 3e2485475279f02464de0efff6df67c7d7caf6cf Mon Sep 17 00:00:00 2001 From: ssh24 Date: Tue, 20 Jun 2017 18:44:29 -0400 Subject: [PATCH] Allow different forms of regexp on like/nlike op As a regexp string As a regexp literal As a regexp object --- lib/mongodb.js | 12 +++- test/mongodb.test.js | 163 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 2 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 1a5af7264..d0cfb1fc6 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -736,9 +736,17 @@ MongoDB.prototype.buildWhere = function(model, where) { }), }; } else if (spec === 'like') { - query[k] = { $regex: new RegExp(cond, options) }; + if (cond instanceof RegExp) { + query[k] = { $regex: cond }; + } else { + query[k] = { $regex: new RegExp(cond, options) }; + } } else if (spec === 'nlike') { - query[k] = { $not: new RegExp(cond, options) }; + if (cond instanceof RegExp) { + query[k] = { $not: cond }; + } else { + query[k] = { $not: new RegExp(cond, options) }; + } } else if (spec === 'neq') { query[k] = { $ne: cond }; } else if (spec === 'regexp') { diff --git a/test/mongodb.test.js b/test/mongodb.test.js index 41960ea21..5e0d768f1 100644 --- a/test/mongodb.test.js +++ b/test/mongodb.test.js @@ -2367,6 +2367,169 @@ describe('mongodb connector', function() { }); }); + context('like and nlike operator', function() { + before(function deleteExistingTestFixtures(done) { + Post.destroyAll(done); + }); + beforeEach(function createTestFixtures(done) { + Post.create([ + { title: 'a', content: 'AAA' }, + { title: 'b', content: 'BBB' }, + ], done); + }); + after(function deleteTestFixtures(done) { + Post.destroyAll(done); + }); + + context('like operator', function() { + context('with regex strings', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: '^A' }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: '^a', options: 'i' }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + }); + + context('with regex literals', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: /^A/ }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: /^a/i }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + }); + + context('with regex object', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: new RegExp(/^A/) }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { like: new RegExp(/^a/i) }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); + }); + }); + }); + }); + + context('nlike operator', function() { + context('with regex strings', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: '^A' }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: '^a', options: 'i' }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + }); + + context('with regex literals', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: /^A/ }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: /^a/i }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + }); + + context('with regex object', function() { + context('using no flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: new RegExp(/^A/) }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + + context('using flags', function() { + it('should work', function(done) { + Post.find({ where: { content: { nlike: new RegExp(/^a/i) }}}, function(err, posts) { + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('BBB'); + done(); + }); + }); + }); + }); + }); + }); + after(function(done) { User.destroyAll(function() { Post.destroyAll(function() {