MongoDB Regular Expression

Source: Internet
Author: User
Tags tutorialspoint

A regular expression is a string that is used to describe and match a series of strings that conform to a certain syntactic rule.

Many programming languages support the use of regular expressions for string manipulation.

MongoDB uses the $regex operator to set a regular expression that matches a string.

MongoDB uses Pcre (Perl Compatible Regular expression) as the regular expression language.

Unlike full-text indexing, we don't need to do any configuration with regular expressions.

Consider the document structure of the following posts collection, which contains the article content and tags:

{   "Post_text": "Enjoy the MongoDB articles on Tutorialspoint",   "tags": [      "MongoDB",      "Tutorialspoint"   ]}
Using regular expressions

The following command uses a regular expression to find an article that contains a w3cschool.cn string:

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

The above query can also be written as:

>db.posts.find ({post_text:/w3cschool.cn/})
Case-insensitive regular expressions

If the retrieval needs to be case insensitive, we can set the $options to $i.

The following command looks for a case-insensitive string, w3cschool.cn:

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

All data containing the string w3cschool.cn are returned in the collection and are not case-sensitive:

{   "_id": ObjectId ("53493d37d852429c10000004"),   "Post_text": "hey! This was my post on  w3cschool.cc ",    
Array elements use regular expressions

We can also use regular expressions in array fields to find content. This is useful in the implementation of the tag, if you need to find the label data that contains the beginning of tutorial (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 fields in your document are indexed, then using an index to find all data queries is faster than regular expression matching.
    • If the regular expression is a prefix expression, all matching data begins with the specified prefix string. For example, if the regular expression is ^tut , the query statement looks for a string that begins with Tut.

MongoDB Regular Expression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.