In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use MongoDB regular expressions, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Regular expressions are often used to search for any pattern or text of a string in all languages. MongoDB also provides a regular expression feature for string patterns using the regular expression $regex operator. MongoDB uses PCRE (Perl compatible regular expressions) as the regular expression language.
Unlike text search, we can use regular expressions directly without any configuration or commands.
Consider a collection of posts with tags after the text. The document structure is as follows:
{"post_text": "enjoy the mongodb articles on yiibai", "tags": ["mongodb", "yiibai"]}
Express using regular expressions
The following regular expression query searches all posts that contain the string yiibai.com:
The copy code is as follows:
> db.posts.find ({post_text: {$regex: "yiibai.com"}})
The same query can also be written as:
> db.posts.find ({post_text:/yiibai.com/})
Using regular expressions is case-insensitive
To make the search case-insensitive, we use $options with the value parameter $I. The following command searches for the string: yiibai.com, regardless of case:
The copy code is as follows:
> db.posts.find ({post_text: {$regex: "yiibai", $options: "$I"}})
The result of the re-adjustment of the query is that it contains the word yiibai document under the size, as follows:
{"_ id": ObjectId ("53493d37d852429c10000004"), "post_text": "hey! this is my post on Yiibai", "tags": ["yiibai"]}
Array elements that use regular expressions:
We can also use the concept of regular expressions in array fields. At this time, it is particularly important for us to realize the function of the tag. So, if you want to search for all posts with tags starting with the phrase tutorial (whether it's tutorial or tutorials or tutorialjava or tutorialphp), you can use the following code:
The copy code is as follows:
> db.posts.find ({tags: {$regex: "tutorial"}})
Optimize regular expression queries:
If the document field is indexed, the query uses a matching regular expression that uses the index value. This makes the search very fast, with regular expressions relative to scanning the entire collection.
If the regular expression is a prefix expression, all matches start with a string of characters. For example, if the regular expression ^ tut, the query has to search only for those that start with the string tut.
Application of mongodb regular expression
Regular expressions are fully supported in mongodb. The operator $regex 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 are completely equivalent, you can directly use regular expressions or operators for the field, that is, the 'name' key in the above example, and the optional item is I, that is, case is ignored.
With regard to regular options, mongodb is slightly different from other language standards and has its own standards.
Optional value of $options
I ignore case
M multi-line lookup. If there is no newline symbol in the content (for example,\ n) or there is no (start/end) in construction, this option has no effect.
The x white space character is completely ignored except for those that are escaped or in the character class, and all characters between # and the next newline character, including both ends, outside the unescaped character class are ignored.
The s-dot metacharacter (.) matches all characters, including newline characters
Suppose we have a database named mongoDemo
Use mongoDemo
There is a collection in the database named lnmopy
Db.lnmopy.find ()
The data are as follows:
{"_ id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "www.lnmopy.com", "site": "website", "tag":
{"_ id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "demo.lnmopy.com", "site": "unknown", "tag": "dpjime mmemo"}
{"_ id": ObjectId ("502dd63d16a25b1ff6000000"), "name": "welcome.lnmopy.com", "site": "website", "tag":
Mongodb's regular expressions support only the javascript native writing of I and m (such as / * .lnmopy.com / I). If you want to use the x and s options, you must use the "$regex" operator and specify the option in "$options".
Update operations using regular expressions:
Db.lnmopy.update ({'name': / * .lnmopy.com/i}, {$set: {' site':'www.lnmopy.com'}})
It means that find the current database named "lnmopy" in the collection of "name" field in accordance with the "/ * .lnmopy.com / I" regular entries, and only update the "site" field to "www.lnmopy.com", this update statement updates only one piece of data, if not with $set, then this record will only be your updated part and the default ObjectId, can be said to be replaced. If you want to replace all, you can add parameters:
Db.lnmopy.update ({'name': / * .lnmopy.com/i}, {$set: {' site':'www.lnmopy.com'}}, false, true)
The parameters are in order, false is upsert, if not, insert the new one. True is the result of multi multiple record updates and all matches. Or specify {multi: true} directly:
Db.lnmopy.update ({'name': / * .lnmopy.com/i}, {$set: {' site':'www.lnmopy.com'}}, {multi: true})
This updates all the "site" fields to "www.lnmopy.com".
There is a flaw in the field "tag" that I designed, that is, it was originally a word, and now each letter is separated by ",". There are similar problems in practical work. Due to batch conversion of data, improper operation of other programs, or changes in business requirements, some fields need to be processed in regular batches, and the general update method of mongodb cannot be implemented, so you need to use javascript statements.
The regular expression replaces the','in the query result for "
Db.lnmopy.find () .forEach (function (u) {u.tag = u.tag.replace (/\, /, "); db.lnmopy.save (u);})
Final execution
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"}
Postscript: javascript is a major feature of mongodb, but also an advantage, a lot of complex queries and processing can be implemented in javascript, it should be noted that the efficiency of javascript is low, in principle, we should try to avoid a large number of use in the main business logic. Analogically speaking, javascript is the equivalent of a stored procedure for oracle, and it's not surprising that 10gen (mongodb's development team) is derived from oracle.
After reading the above, do you have any further understanding of how to use MongoDB's regular expressions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.