$lookup lab: $match syntax

I’m working on the $lookup lab from Chapter 3, and I’ve found here on the forum that many people are starting with a $match stage like this on the air_routes collection:

var pipeline = [
  {
    $match: {
      airplane: /747|380/
    }
  }
]

I cannot for the life of me find any documentation about this : /pattern|pattern/ syntax. It works, but why? Is it documented anywhere? All I can find is documentation about the $regex operator.

1 Like

In the documentation at https://docs.mongodb.com/manual/reference/operator/query/regex/ it is mentioned that

MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.42 with UTF-8 support.

Right — but the $regex documentation doesn’t mention using this syntax on a $match expression, and neither does the $match documentation, but it’s used in the solution here. Am I missing something?

Yes , may be the following from the documentation can be more explicit.

Definition

$regex

Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.42 with UTF-8 support.

Hi @Andrew_Childress,

The /pattern/ syntax is JavaScript’s shorthand notation for creating a regular expression literal.

This syntax is mentioned near the top of the $regex operator documentation after the first three syntax examples:

In MongoDB, you can also use regular expression objects (i.e. /pattern/ ) to specify regular expressions:

   { <field>: /pattern/<options> }

For restrictions on particular syntax use, see $regex vs. /pattern/ Syntax.

This is equivalent to:

 airplane: { $regex: /747|380/ }

or

 airplane: { $regex: '747|380' }

Regards,
Stennie

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.