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

MongoDB like operator not working properly #9219

Closed
Serkan80 opened this issue May 11, 2020 · 7 comments · Fixed by #9237
Closed

MongoDB like operator not working properly #9219

Serkan80 opened this issue May 11, 2020 · 7 comments · Fixed by #9237
Assignees
Labels
area/mongodb kind/bug Something isn't working
Milestone

Comments

@Serkan80
Copy link

Serkan80 commented May 11, 2020

Describe the bug
I have the following Books in my collection:

  • Book(title=Harry Potter, author=JK Rowling)
  • Book(title=IT, author=Stephen King)

And when I do the following query:

Book.list("title like ?1", format("/.*%s.*/i, "harr"));

I see the following on my console printed out:

{'title':{'$regex':'/.*harr.*/i'}} 

And I get 0 result back.

Expected behavior
I expect 1 result.

Environment (please complete the following information):

  • Linux lenovo 5.4.0-7626-generic Arc - make it possible to get interceptor bindings from #30158816988320.04~bbe668a-Ubuntu SMP Wed Apr 29 21:00:02 UTC x86_64 x86_64 x86_64 GNU/Linux.
  • openjdk version "11.0.7" 2020-04-14
    OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10)
    OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)
  • Quarkus 1.4.2.Final
  • Apache Maven 3.6.3

Additional context
The problem lies in the generated query.
If I write the following query, then it works:

Book.list(format("{title: {$regex: /.*%s.*/i}}", title));

Notice the quotes !

See also:
https://stackoverflow.com/questions/61732717/mongodb-panache-queries-not-returning-any-result/61733115

@Serkan80 Serkan80 added the kind/bug Something isn't working label May 11, 2020
@quarkusbot
Copy link

/cc @loicmathieu

@loicmathieu
Copy link
Contributor

@Serkan80 as I understand it, using like generates an incorrect query with single quotes around the $regex operator.

Book.list("title like ?1", format("/.*%s.*/i, "harr"));

Generates

{'title':{'$regex':'/.*harr.*/i'}} 

Where it should be

{'title':{$regex:'/.*harr.*/i'}} 

@Serkan80
Copy link
Author

Serkan80 commented May 11, 2020

@Serkan80 as I understand it, using like generates an incorrect query with single quotes around the $regex operator.

Book.list("title like ?1", format("/.*%s.*/i, "harr"));

Generates

{'title':{'$regex':'/.*harr.*/i'}} 

Where it should be

{'title':{$regex:'/.*harr.*/i'}} 

No not correct, it should be either like this:
{title: {$regex: /.*harr.*/i}}
or like this:
{'title': {$regex: /.*harr.*/i}}

No single quotes on the value of the regex (/.harr./i).

@loicmathieu
Copy link
Contributor

@Serkan80 escaping with single quotes the operator is not the issue.
The issue is with the regex itself. You can define regex in two ways in MongoDB:

  • Using a regular string => '.*harr.*' in this case (but you will be missing the i option for ignore case)
  • Using a JavaScript regex => /.*harr.*/ with this form you can add options to your regexes

We automatically escape the value of the parameter so you can ony use regular String for the moment.

@loicmathieu
Copy link
Contributor

So, to fix this we need, for the like operator, to check if the value of the parameter is a JavaScript regex and don't escape in this case.

@Serkan80
Copy link
Author

Serkan80 commented May 11, 2020

So, to fix this we need, for the like operator, to check if the value of the parameter is a JavaScript regex and don't escape in this case.

Ok, then you need to document this well, because people have to know that you can use 2 types of regex. And only the JS one support case-insensitivity (and other options).

It would be nice to do it just 1 way with the options support.

For example:

list("title like ?1", ".*myQuery.*", Options.IGNORECASE, Options.OtherOption, etc.); 

And when you see that there are options provided, you just generare the JS variant, otherwise the default/current one.

@loicmathieu
Copy link
Contributor

people have to know that you can use 2 types of regex

I guess that if people try to use one form or the other it's because they know they exist ;)

Unfortunatly, we cannot easily add options to the list method as you requested as the varags is for the parameters ...

So the best I can propose is to check if it's a JavaScript regex and don't escape in this case.

@loicmathieu loicmathieu self-assigned this May 12, 2020
loicmathieu added a commit to loicmathieu/quarkus that referenced this issue May 12, 2020
loicmathieu added a commit to loicmathieu/quarkus that referenced this issue May 12, 2020
@gsmet gsmet added this to the 1.5.0 milestone May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/mongodb kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants