Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in MongoDB

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

It is believed that many inexperienced people have no idea about how to use regular expressions in MongoDB. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

If you want to implement fuzzy queries, you must use regular expressions, in the form of regular expressions that are compatible with Perl.

If you want to implement a regular expression, follow the defined format as follows:

● basic syntax: {key: regular expression}

● complete syntax: {key: {"$regex": regular tag, "$options": option}}.

For options, the main thing is to set the flag for regular information query:

● "I": ignore the case of letters, most commonly used

● "m": multiline lookup

● "x": blank strings are completely ignored except for those that are escaped or are in the character class

● "s": matches all characters ("."), including line breaks.

It should be noted that if used directly, only I and m can be used, while "x" and "s" must use "$regex"

Example: query personnel information that begins with "Wang"

> db.emp.find ({"name": / Wang /}) .pretty ()

{

"_ id": ObjectId ("599108433268c8e84253be2d")

"name": "bastards"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

}

Example: query the information of the person whose name has the letter a

> db.emp.find ({"name": / a name I}) .pretty ()

{

"_ id": ObjectId ("599129a00184ff511bf02b87")

"name": "Liu A"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

}

{

"_ id": ObjectId ("599148bd0184ff511bf02b91")

"name": "Lin A"

"sex": "male"

"age": 22

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

"parents": [

{

"name": "Lin A Father"

"age": 50

Job: the Peasant

}

{

"name": "Lin A Mother"

"age": 49

"job": "worker

}

]

}

If you want to perform the operation of a fuzzy query, strictly speaking, you only need to write a keyword.

> db.emp.find ({"name": {"$regex": / a regex}}) .pretty ()

{

"_ id": ObjectId ("599129a00184ff511bf02b87")

"name": "Liu A"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

}

{

"_ id": ObjectId ("599148bd0184ff511bf02b91")

"name": "Lin A"

"sex": "male"

"age": 22

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

"parents": [

{

"name": "Lin A Father"

"age": 50

Job: the Peasant

}

{

"name": "Lin A Mother"

"age": 49

"job": "worker

}

]

}

In the regular operation, in addition to querying the contents of a single field, you can also query array data.

Example: query array data

> db.emp.find ({"course": / language / I}) .pretty ()

{

"_ id": ObjectId ("599129a00184ff511bf02b87")

"name": "Liu A"

"sex": "male"

"age": 35

"sal": 8000

"loc": "Beijing"

"course": [

"Chinese"

"Mathematics"

"English"

"Music"

"politics"

]

}

There is a slight difference between the regular symbols in MongoDB and the previous Java regularities. It is not recommended to use some of the previous tags, and the regularities are applied to the query of fuzzy data.

After reading the above, have you mastered how to use regular expressions in MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report