Home > Software engineering >  how to search for a substring in MongoDB which starts with
how to search for a substring in MongoDB which starts with

Time:09-19

From a mongoDB collection I wanted to search the document for which the name field has substring " Ava"(substring starting with ). Tried it by using regex but it was giving following error: "errmsg" : "Regular expression is invalid: nothing to repeat"

Query made : db.employees.find({name:{$regex : " ava", $options: 'i'}})

Also tried this query but it also gave the same error db.employees.find({name:/ ava/})

CodePudding user response:

is a special character, you just need to escape it, like so:

db.employees.find({name:/\ ava/})
  • Related