1

I am running the query given below.

db.CollectionName.find({'serviceName':{'$regex':'^(ALBUMIN$','$options':'si'}})

I am getting the following error.

Error: error: {
    "ok" : 0,
    "errmsg" : "Regular expression is invalid: missing )",
    "code" : 2,
    "codeName" : "BadValue"
}

How can I avoid this error since bracket can be anywhere inside the string? Like- (ALBUMIN or ((ALBUMIN) etc.

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
Manshavi Kumar
  • 133
  • 2
  • 9

1 Answers1

2

You need to escape the ( character. You can use \ for escaping special characters in a regex. So for your case it would be like

db.CollectionName.find({'serviceName':{'$regex':'^\\(ALBUMIN$','$options':'si'}})

You can always test your regular expressions on https://regex101.com/ whenever you use it.

Kamesh
  • 1,122
  • 9
  • 12
  • I tried your solution but still i am getting the same error. – Manshavi Kumar Sep 18 '18 at 13:47
  • Aah I just missed that in javascript you need to have \\ for considering it as one. Basically we need to escape \ as well in javascript. I have edited my answer – Kamesh Sep 18 '18 at 13:48