Regular Expressions are often used in all the languages to search for a pattern or word in any string. MongoDB also provides regular expression functionality for string pattern matching using $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as regular expression language.
Unlike text search, there is no need to do any configuration or command to use regular expressions.
Consider below document structure under posts collection containing post text and its tags
Below regex query will search for all the posts consisting of string wisdomjobs in it
Same query can also be written as
$options parameter with value $i can be used to make the search case insensitive. Below command will look for strings which have the word wisdomjobs, irrespective of smaller or capital case
One of the results returned from this query is the below document which includes the word wisdomjobs in different cases
Regex concept can also be used on array field which is particularly very important when the functionality of tags is implemented. Therefore, if you want to search for all the posts which have tags beginning from the word tutorial (either tutorial or tutorials or wisdomjobs or tutorialphp), use below code
MongoDB Related Interview Questions
|Python Interview Questions||C & Data Structures Interview Questions|
|Hadoop Interview Questions||Node.js Interview Questions|
|Scala Interview Questions||Socket Programming Interview Questions|
|HBase Interview Questions||Solaris Administrator Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.