How can I escape "(" in regex literals?

Problem

Using the mongo command-line tool, I want to fetch documents containing " (" in a string field using a regex literal, like this:

{writers: / \(/}

However this fails in the mongo command line tool (v4.4.0), presumably because it’s looking for a closing parentheses ) despite ( being escaped:

enter image description here

What I’ve tried / Workarounds

  1. The expression works fine in Compass:

enter image description here

  1. I can express ( using its octal escape sequence {writers: / \50/}:

enter image description here

  1. I can use {regex...} with a string literal:

enter image description here

Version

I’m using mongo 4.2.9 and 4.4.0:

enter image description here

Is there a way to match “(” characters in regex literals without resorting such workarounds?

/.../ is a much cleaner syntax.

Hi @jimu,

You can refer to the following link for regex expressions:

If you can elaborate the output that you want to achieve, I can help you better here.

Please feel free to reach out if you have any additional questions.

Thanks,
Sonali

Steps to reproduce

mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
use videos
db.movies.find({writers: / \(/})

Expected Output

{ "_id" : ObjectId("573a1390f29313caabcd41f0"), "title" : "Ella Lola, a la Trilby", "year" : 1898, "cast" : [ "Ella Lola" ], "plot" : "Dancer Ella Lola dances a routine based on the famous character of \"Trilby\".", "fullplot" : "Dancer Ella Lola dances a routine based on the famous character of \"Trilby\".", "lastupdated" : "2015-08-29 00:19:44.140000000", "type" : "movie", "directors" : [ "James H. White" ], "writers" : [ "George L. Du Maurier (novel)" ], "imdb" : { "rating" : 4.7, "votes" : 83, "id" : 192 }, "countries" : [ "USA" ], "genres" : [ "Short" ], "tomatoes" : { "viewer" : { "rating" : 4.1, "numReviews" : 46 }, "lastUpdated" : ISODate("2015-09-14T17:32:55Z") }, "num_mflix_comments" : 1, "comments" : [ { "name" : "Tyrion Lannister", "email" : "peter_dinklage@gameofthron.es", "movie_id" : ObjectId("573a1390f29313caabcd41f0"), "text" : "Ipsa dignissimos quibusdam id doloremque quis corrupti placeat. Fugit velit natus nesciunt iure impedit at aut. Doloribus unde dolores deleniti aspernatur eos.", "date" : ISODate("1996-06-11T07:01:30Z") } ] }
...

Actual Output

...
...

Steps I’ve tried

see original post

I’ve tried various ways of escaping the ( but the problem remains. See original post.

This appears to be a bug in the mongo command line tool. If this is the case, please confirm so.

Hi @jimu,

When you use this connection string, you will have access to the aggregations database. The m121 user only has permission to access this database in the cluster.

Generally in order to use a literal backslash anywhere in a regex, it must be escaped by another backslash.

There are certain restriction while using PCRE expression like /pattern/, please refer to the documentation below:

I would recommend you to post your query in MongoDB Community Discussion forums for an in-depth discussion.

Thanks,
Sonali

Try the alternative syntax:

db.movies.find( { "writers" : { "$regex" : " \\(" } }