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

Regression in string matching in 4.3 #355

Closed
JoelCourtney opened this issue Jul 20, 2023 · 7 comments
Closed

Regression in string matching in 4.3 #355

JoelCourtney opened this issue Jul 20, 2023 · 7 comments
Labels

Comments

@JoelCourtney
Copy link

I am trying to match a string with regex, but with an exception for a specific string like so:

let query = new Query({
  str: {
    $regex: ".*MATCH",
    $not: "NOT_A_MATCH"
  }
});

console.log(query.test({
  str: "NOT_A_MATCH"
}));
// expected: false

In 4.2 this query correctly returns false. In 4.3 and later this now returns true. In this simple case I can workaround by removing the $not and making the regex a little more complex, but this still seems like a bug.

@JoelCourtney JoelCourtney changed the title Regression in string matching since 4.3 Regression in string matching in 4.3 Jul 20, 2023
@kofrasa
Copy link
Owner

kofrasa commented Jul 23, 2023

Hi @JoelCourtney, thanks for the report. Your query is incorrect but due to weak input validation the library does not throw an error. Only a single operator is processed when specified for a field, which in this will be the $regex operator.

The string "NOT_A_MATCH" does match the regex /.*MATCH/, so the expected return is true.

The behaviour in 4.2.0 was incorrect.

Also the value for $not is invalid see https://www.mongodb.com/docs/manual/reference/operator/query/not/.

@kofrasa kofrasa closed this as completed Jul 26, 2023
@JoelCourtney
Copy link
Author

JoelCourtney commented Aug 2, 2023

@kofrasa Thanks for responding. My bad with the $not operator. If I'm understanding you right though, I'd expect this query to behave oddly too:

let query = new Query({
  num: {
    $gt: 0,
    $ne: 5
  }
});

console.log(query.test({
  num // see below
}));

When num: 0, it prints false, when num: 3 it prints true, and when num: 5 it prints false. This seems to mean that both operators are being applied and implicitly and'ed together. Is this a bug?

EDIT num: 0 used to be num: 1. That was a typo.

@kofrasa
Copy link
Owner

kofrasa commented Aug 4, 2023

Both operators are applied and ANDed together however the results you described is wrong. The outputs should be true for both 1 and 3, and false for 5. Perhaps you are still testing on 4.3.0. The bug if fixed in the new version. You can run the test here. https://runkit.com/embed/6py5m6di0nl5

@JoelCourtney
Copy link
Author

JoelCourtney commented Aug 4, 2023

Ah that was a typo, I meant 0 instead of 1. Regardless, this still doesn't make sense:

Only a single operator is processed when specified for a field

Both operators are applied and ANDed together

Those statements seem to directly contradict each other. Why does this query only apply one operator and ignores the other:

str: {
  $regex: ".*MATCH",
  $ne: "NOT_A_MATCH"
}

But this query applies both:

num: {
  $gt: 0,
  $ne: 5
}

@kofrasa
Copy link
Owner

kofrasa commented Aug 4, 2023

Sorry for the confusion. The first statement is false in the context of using the Query object. All the operators in the expression are processed and the results ANDed together.

The first statement is only true for pipeline and expression operators.

@JoelCourtney
Copy link
Author

JoelCourtney commented Aug 9, 2023

Thanks for clarifying. Then do you agree that there is a bug here? Here's a section from the mongo docs that uses the exact same pattern that I'm trying to use. If you use this query in mingo 4.3 or higher: { name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }, and test the object {name: 'acmeblahcorp'}, it incorrectly returns true. In 4.2 it returns false.

@kofrasa
Copy link
Owner

kofrasa commented Aug 11, 2023

Yes there is a bug. I see what you mean. The behaviour is inconsistent specifically with $regex matching.

@kofrasa kofrasa reopened this Aug 11, 2023
@kofrasa kofrasa added the bug label Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants