question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling $regex against a property that does not exist on the collection evaluates it to "True"

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
mssodhicommented, Aug 18, 2022

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-strict

Fortunately, you can disable it per query like this:

await UserModel.find({
  $or: [{ email: 'a@b.com' }, { email_path_dne: { $regex: 'abcd' } }],
}, null, { strictQuery: false });

or globally like this mongoose.set('strictQuery', false);

0reactions
github-actions[bot]commented, Sep 13, 2022

This issue was closed because it has been inactive for 19 days since being marked as stale.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found