Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex options /i, etc not working in MongoDB Compass

This question is related to MongoDB Compass Community Version 1.17.0.

Code { location: /IL$|US$/i } (with regex options) works with the Documents Filter tool, but with the Aggregations $match pipeline stage the application says Expected "[" or AggregationStage but "{" found.

Code with no regex options, i.e., { location: /IL$|US$/ } works in both the filter, and the aggregation tools.

How can I use regex options in the $match pipeline stage of MongoDB Compass?

like image 482
Vladimir Avatar asked May 05 '19 12:05

Vladimir


People also ask

How regex works in MongoDB?

MongoDB provides the functionality to search a pattern in a string during a query by writing a regular expression. A regular expression is a generalized way to match patterns with sequences of characters. MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support.

Does MongoDB support regex?

MongoDB uses Perl compatible regular expressions (i.e. "PCRE" ) version 8.42 with UTF-8 support. For restrictions on particular syntax use, see $regex vs. /pattern/ Syntax.

What is regex in mongoose?

Regular expression or Regex is a very important part of programming. It is usually used to find a pattern in a string.

How do I use $in in MongoDB?

MongoDB provides different types of comparison query operators and $in operator is one of them. This operator is used to select those documents where the value of the field is equal to any of the given value in the array.


1 Answers

You can use the RegExp object, e.g.:

{
  email: RegExp('@gmail.com$', 'i')
}
like image 119
Massimiliano Marcon Avatar answered Sep 27 '22 00:09

Massimiliano Marcon