Regular Expression in MongoDB

Regular Expression is used frequently in all languages ​​to search for a pattern or a word in any string. MongoDB also provides Regular Expression to match Pattern matching in string by using $ regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as the Regular Expression language.

Unlike Text Search, we do not need to execute any command or configuration to use Regular Expression.

You follow the structure of the Document under the posts collection containing the following posts_text and tags fields:

 { "post_text" : "enjoy the mongodb articles on tutorialspoint" , "tags" : [ "mongodb" , "tutorialspoint" ] } 

Use regex in MongoDB

Query the following regex to search all posts that contain the tutorialspoint string in it:

 > db . posts . find ({ post_text :{ $regex : "tutorialspoint" }}) 

The same query can be written as follows:

 > db . posts . find ({ post_text : /tutorialspoint/ }) 

Use regex with options that are not case sensitive

To make the search caseless, use the $ options parameter with the value $ i . The following command will search for strings that have the word tutorialspoint, regardless of whether they are uppercase or lowercase.

 > db . posts . find ({ post_text :{ $regex : "tutorialspoint" , $options : "$i" }}) 

One of the results of this query is the following Document which contains the word tutorialspoint in different typefaces:

 { "_id" : ObjectId ( "53493d37d852429c10000004" ), "post_text" : "hey! this is my post on TutorialsPoint" , "tags" : [ "tutorialspoint" ] } 

Use regex for array elements

We can also use the term regex on the array field. This is especially important when we implement the features of tags. Therefore, if you want to search all posts with tags starting with the tutorial word, you can use the following code:

 > db . posts . find ({ tags :{ $regex : "tutorial" }}) 

Optimize queries for Regular Expression

If the Document fields are already indexed , the query will use these indexed values ​​to match the Regular Expressions. This makes searching faster when compared to scanning the entire Collection.

If Regular Expression is a Prefix expression, all matches are started with a specific string. For example, if the regex is ^ tut, the query will only find strings that start with the tut.

According to Tutorialspoint

Previous post: Text Search in MongoDB

Next article: Working with Rockmongo

4.3 ★ | 4 Vote

May be interested

  • Advantages of MongoDBAdvantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Query Document in MongoDBQuery Document in MongoDB
    to query data from collection in mongodb, you need to use the find () method in mongodb.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • ObjectId in MongoDBObjectId in MongoDB
    you have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.