Skip to content

Instantly share code, notes, and snippets.

@hrishi7
Created July 27, 2020 13:32
Show Gist options
  • Save hrishi7/517bd1596f2bb5a880cae8b54bbadb30 to your computer and use it in GitHub Desktop.
Save hrishi7/517bd1596f2bb5a880cae8b54bbadb30 to your computer and use it in GitHub Desktop.
21
I was having same problem with my title and after lots of searching, i found this answer Here,
var search = 'Joe';
db.users.find(name: /^search/)
db.users.find(name: {$regex: /^search/});
db.users.find(name: {$regex: "/^" + search + "/"});
The queries above won’t return anything. The solution to this little problem is quite simple:
db.users.find(name: new RegExp(search)) //For substring search, case sensitive.
db.users.find(name: new RegExp('^' + search + '$')) //For exact search, case sensitive
db.users.find(name: new RegExp(search, ‘i')) //For substring search, case insensitive
db.users.find(name: new RegExp('^' +search + '$', 'i')); //For exact search, case insensitiv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment