Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimongo bug with $regex #2817

Closed
fpoirier1 opened this issue Oct 14, 2014 · 3 comments
Closed

Minimongo bug with $regex #2817

fpoirier1 opened this issue Oct 14, 2014 · 3 comments

Comments

@fpoirier1
Copy link

Hello,

I have a project where the user can search his products using a search field. I'm using a Regex to query the products by name. There seems to be a bug for products with the same name. There is only one returned. My helper function looks like this :

products : function(){
  var query = Session.get('productsList.query');

  if(!query || query.length < 2)
    return Products.find();

  var regex = new RegExp(query, 'gi');
  return Products.find({name : {$regex : regex}});
},

Here is the reproduction info :

I tried using the full name without the regex and it returns all the corresponding products. Also, the regex works fine in MongoDB.

@Slava
Copy link
Contributor

Slava commented Oct 15, 2014

I believe you intended to either query { name: regex } or { name: { $regex: query } }. I am not completely sure what should happen when you ask to take a regex as an argument for $regex operator.

@Slava
Copy link
Contributor

Slava commented Oct 15, 2014

After stepping through the minimongo code with a debugger, it looks like you get this behavior because of the g flag in your regexp. The global flag tells regexp to keep a state so when called on the same string, the regexp should start from the position it finished last time (in this case "Shorts" and "Shorts" is the same string in JS terminology).

Slava added a commit that referenced this issue Oct 15, 2014
@Slava Slava closed this as completed in 650ca70 Oct 15, 2014
@Slava
Copy link
Contributor

Slava commented Oct 15, 2014

@fpoirier1 thank you, this is fixed on devel now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants