Calling $regex against a property that does not exist on the collection evaluates it to "True"
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.5.2
Node.js version
14.18.2
MongoDB server version
5.0.10
Description
When querying a collection through mongoose using a $regex command if the field does not exist, it will treat it as “true” so that in the case of an $or :[$regex:{}] that block would always be treated as “true”
let conditions = {
$or: [
{
_id: entity._id.toString(),
},
path: {
$regex: `${entity._id.toString()}`,
},
]}
In the above condition, the _id would never need to be matched if the “path” property did not exist on the collection.
This didn’t used to work as expected on mongoose versions 5.x.x and also through Mongo Compass.
Steps to Reproduce
Create a new collection with properties
- firstName
- lastName
Add some data
send through the query:
let conditions = {
$or: [
{
firstName: 'i do not exist',
},
i_do_not_exist: {
$regex: `randomValue`,
},
]}
When doing so it would return back all the rows of data from the collection
Expected Behavior
if a property does not exist on the collection for the $regex, it should just evaluate it to “False” like Mongo Compass and previous versions of Mongoose.
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
Regular expression to match a line that doesn't contain a word
The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds: ^((?!hede).)*$.
Read more >Best Practices for Regular Expressions in .NET - Microsoft Learn
Consider the input source. In general, regular expressions can accept two types of input: constrained or unconstrained.
Read more >Regular Expression in Java - Java Regex Example
Regular Expression can be used to search, edit or manipulate text. A regular expression is not language specific but they differ slightly for ......
Read more >perlre - Perl regular expressions - Perldoc Browser
Regular expressions are strings with the very particular syntax and meaning described in ... We call this "matching" the target string against the...
Read more >RegExp.prototype.test() - JavaScript - MDN Web Docs
true if there is a match between the regular expression and the string str . ... the calling regex's lastIndex property will reset...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Unfortunately this is the expected behavior.
As of Mongoose 6.0.10, we brought back the strictQuery option. However, strictQuery is tied to strict by default. This means that, by default, Mongoose will filter out query filter properties that are not in the schema.
https://mongoosejs.com/docs/migrating_to_6.html#strictquery-is-removed-and-replaced-by-strictFortunately, you can disable it per query like this:
or globally like this
mongoose.set('strictQuery', false);
This issue was closed because it has been inactive for 19 days since being marked as stale.