7

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?

Vladimir
  • 145
  • 2
  • 8

1 Answers1

15

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

{
  email: RegExp('@gmail.com$', 'i')
}
  • What version of Compass are you using? I just tried the latest stable (1.20.3) and it seems to work fine. – Massimiliano Marcon Dec 19 '19 at 17:19
  • it works for me by changing the indentation and keeping everything on one line. (Compass Version 1.21.2) – Riccardo May 22 '20 at 10:38
  • 1
    I noticed it doesn't work if you have more than one option in a single string. For example: `'ig'` needs to be broken up into `'i', 'g'`. – Benji Jun 01 '22 at 14:13
  • I also noticed that if the `RegExp()` query is not responding you have to sometimes close the tab for the collection, re-open it, and try the query again in a new tab to get it to work with the fixed query. I just reported this as a bug in jira.mongodb.org – Benji Aug 30 '22 at 16:30