- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB - Regular Expression
Regular Expressions are frequently used in all languages to search for a pattern or word in any string. MongoDB also provides functionality of regular expression for string pattern matching using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as regular expression language.
Unlike text search, we do not need to do any configuration or command to use regular expressions.
Assume we have inserted a document in a database named posts as shown below −
> db.posts.insert( { "post_text": "enjoy the mongodb articles on tutorialspoint", "tags": [ "mongodb", "tutorialspoint" ] } WriteResult({ "nInserted" : 1 })
Using regex Expression
The following regex query searches for all the posts containing string tutorialspoint in it −
> db.posts.find({post_text:{$regex:"tutorialspoint"}}).pretty() { "_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"), "post_text" : "enjoy the mongodb articles on tutorialspoint", "tags" : [ "mongodb", "tutorialspoint" ] } { "_id" : ObjectId("5dd7d111f1dd4583e7103fe2"), "post_text" : "enjoy the mongodb articles on tutorialspoint", "tags" : [ "mongodb", "tutorialspoint" ] } >
The same query can also be written as −
>db.posts.find({post_text:/tutorialspoint/})
Using regex Expression with Case Insensitive
To make the search case insensitive, we use the $options parameter with value $i. The following command will look for strings having the word tutorialspoint, irrespective of smaller or capital case −
>db.posts.find({post_text:{$regex:"tutorialspoint",$options:"$i"}})
One of the results returned from this query is the following document which contains the word tutorialspoint in different cases −
{ "_id" : ObjectId("53493d37d852429c10000004"), "post_text" : "hey! this is my post on TutorialsPoint", "tags" : [ "tutorialspoint" ] }
Using regex for Array Elements
We can also use the concept of regex on array field. This is particularly very important when we implement the functionality of tags. So, if you want to search for all the posts having tags beginning from the word tutorial (either tutorial or tutorials or tutorialpoint or tutorialphp), you can use the following code −
>db.posts.find({tags:{$regex:"tutorial"}})
Optimizing Regular Expression Queries
If the document fields are indexed, the query will use make use of indexed values to match the regular expression. This makes the search very fast as compared to the regular expression scanning the whole collection.
If the regular expression is a prefix expression, all the matches are meant to start with a certain string characters. For e.g., if the regex expression is ^tut, then the query has to search for only those strings that begin with tut.