regex options /i, etc not working in MongoDB Compass
Asked Answered
S

1

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?

Spectrophotometer answered 5/5, 2019 at 12:12 Comment(0)
L
16

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

{
  email: RegExp('@gmail.com$', 'i')
}
Louiselouisette answered 6/5, 2019 at 12:7 Comment(4)
What version of Compass are you using? I just tried the latest stable (1.20.3) and it seems to work fine.Louiselouisette
it works for me by changing the indentation and keeping everything on one line. (Compass Version 1.21.2)Lashandralashar
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'.Rhythmandblues
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.orgRhythmandblues

© 2022 - 2024 — McMap. All rights reserved.