MongoDB Regular Expressions and Applications

Source: Internet
Author: User
Tags mongodb update

MongoDB Regular Expressions and Applications

Regular Expressions are often used to search for any mode or text of a string in all languages. MongoDB also provides the Regular Expression Function to use the regular expression $ regex operator in string mode. MongoDB uses PCRE (Perl Compatible Regular Expressions) as the regular expression language.

Unlike text search, we can directly use regular expressions without any configuration or command.

Consider the set of posts whose tags contain text. The document structure is as follows:

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

Expression Using Regular Expressions

The following regular expression queries and searches for all posts containing the string yiibai.com:

Copy codeThe Code is as follows:
> Db. posts. find ({post_text: {$ regex: "yiibai.com "}})

The same query can be written as follows:

> Db. posts. find ({post_text:/yiibai.com /})

The regular expression is case insensitive.

To make the search case insensitive, we use the $ options parameter $ I. The following command searches for the string: yiibai.com, regardless of case:

Copy codeThe Code is as follows:
> Db. posts. find ({post_text: {$ regex: "yiibai", $ options: "$ I "}})

The result of the re-adjustment of the query is: The size contains the word yiibai document, as shown below:

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

Array elements using regular expressions:

We can also use the array field regular expression concept. At this time, the tag function is particularly important. Therefore, if you want to search all posts with tags starting with the phrase tutorial (whether tutorial or tutorials or tutorialjava or tutorialphp), you can use the following code:

Copy codeThe Code is as follows:
> Db. posts. find ({tags: {$ regex: "tutorial "}})

Optimize Regular Expression query:

If the document field has been indexed, the query uses the regular expression matching the index value. This makes the search very fast, and the regular expression is relative to the entire set of scan.

If a regular expression is a prefix expression, all matches start with a string of characters. For example, if the regular expression ^ tut is used, only the start tut strings can be searched in the query.

Mongodb Regular Expression Application

Mongodb fully supports regular expressions. The $ regex operator can be used in general queries.

db.lnmopy.find( { 'name': /*.lnmopy.com/i } )db.lnmopy.find( { 'name': { $regex: '*.lnmopy.com', $options: 'i' } } )

The above two types are completely equivalent. You can directly pair the field (field), that is, the 'name' key in the above example, use a regular expression or use an operator. The optional item is I, that is, case-insensitive.
Regular Expressions are slightly different from standard regular expressions in other languages and have their own standards.

Optional values of $ options

I. case insensitive;

M multi-row search. If the content does not contain a line break symbol (for example, \ n) or is not constructed (start/end), this option has no effect;
X blank characters are ignored except escaped or in the character class. All characters except the unescaped character class # And the next line break, including both ends, are ignored;

The s dot metacharacters (.) matches all characters, including line breaks.

Suppose we have a database named consumer demo.

Use existing demo

The database has a collection named lnmopy.

Db. lnmopy. find ()

The following data is available:

{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "www.lnmopy.com", "site": "website", "tag": "l, n, m, o, p, y "}
{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "demo.lnmopy.com", "site": "unknown", "tag": "d, e, m, o "}
{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "welcome.lnmopy.com", "site": "website", "tag": "w, e, l, c, o, m, e "}

Mongodb Regular Expressions only support native javascript Writing of I and m (for example,/* .lnmopy.com/I ). If you use the x and s options, you must use the "$ regex" operator and specify the options in "$ options.

Update operations using regular expressions:

Db. lnmopy. update ({'name':/* .lnmopy.com/I}, {$ set: {'SITE': 'www .lnmopy.com '}});

It indicates that the "name" field in the Set named "lnmopy" in the current database matches "/* Signature. If you want to replace all, you can add the following parameters:

Db. lnmopy. update ({'name':/* .lnmopy.com/I}, {$ set: {'SITE': 'www .lnmopy.com '}, false, true );

The parameters are ordered. If false is set to upsert, a new one is inserted if no one exists. True indicates that multiple records are updated by multi, and all matching results are returned. Or you can directly specify {multi: true }:
Db. lnmopy. update ({'name':/* .lnmopy.com/I}, {$ set: {'SITE': 'www .lnmopy.com '}}, {multi: true });
In this way, all the "site" fields are updated to "www.lnmopy.com.

The field "tag" I designed has a defect, that is, it is a word. Now every letter is separated. Similar problems also exist in actual work. Some fields need to be processed in batches due to reasons such as batch data conversion, improper operation of other programs, or changes to business requirements, generally, mongodb update methods cannot be implemented. In this case, javascript statements are required.

Replace the regular expression with ',' in the query result"

Db. lnmopy. find (). forEach (function (u) {u. tag = u. tag. replace (/\,/, ""); db. lnmopy. save (u );});

Last executed

Db. lnmopy. find ()

The following data is displayed:

{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "www.lnmopy.com", "site": "www.lnmopy.com", "tag": "lnmopy "}
{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "demo.lnmopy.com", "site": "www.lnmopy.com", "tag": "demo "}
{"_ Id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "welcome.lnmopy.com", "site": "www.lnmopy.com", "tag": "welcome "}

Note: javascript is a major feature and advantage of mongodb. Many complex queries and processing can be implemented using javascript. Note that javascript is inefficient, in principle, we should try to avoid extensive use in the main business logic. By analogy, javascript is equivalent to the stored procedure of oracle. It is not surprising that the 10gen (mongodb Development Team) is from oracle. I will write about how to use more complex javascript in the future.

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.