Example Analysis of MongoDB Fuzzy query Operation
This article mainly introduces the example analysis of MongoDB fuzzy query operation, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
1. Function and grammatical description
What a regular expression does: a regular expression uses a specified string to describe and match a series of strings that conform to a syntactic rule. Many programming languages support string manipulation using regular expressions. MongoDB uses the $regex operator to set the regular expression that matches the string.
Grammar one
{: {$regex: / pattern/, $options:''}} {: {$regex: 'pattern', $options:''} {: {$regex: / pattern/}}
Grammar two
{: / pattern/}
two。 Case demonstration
Suppose the message information of our OrderService service is stored in MongoDB, and the data is as follows:
(here are 9 documents used in the demonstration.)
{"_ id": ObjectId ("5d305b1c4857fc49c0c14c81"), "order": "QQ00001", "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b3b4857fc49c0c14c82 ")," order ":" QQ00001 "," data ":"\ "ERR repeat order\"}, {"_ id": ObjectId ("5d305b404857fc49c0c14c83"), "order": "QQ00002" "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b454857fc49c0c14c84 ")," order ":" QQ00002 "," data ":"\ "ERR repeat order\"}, {"_ id": ObjectId ("5d305b4b4857fc49c0c14c85"), "order": "QQ00003", "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b4f4857fc49c0c14c86 ") "order": "QQ00003", "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305bb74857fc49c0c14c87 ")," order ":" QQ00003 "," data ":"\ "OK\"}, {"_ id": ObjectId ("5d305bd14857fc49c0c14c88"), "order": "QQ00002", "data": "\" OK\ "}, {" _ id ": ObjectId (" 5d305be94857fc49c0c14c89 ") "order": "QQ00001", "data": "\" OK\ "}
Requirements: query documents with the character OK in the data field
The execution code is as follows:
Db.dbtestregex.find ({data: {$regex:/OK/}})
\ in the query code is the transfer character, as is the following query statement.
The returned result is as follows:
{"_ id": ObjectId ("5d305bb74857fc49c0c14c87"), "order": "QQ00003", "data": "\" OK\ "}, {" _ id ": ObjectId (" 5d305bd14857fc49c0c14c88 ")," order ":" QQ00002 "," data ":"\ "OK\"}, {"_ id": ObjectId ("5d305be94857fc49c0c14c89"), "order": "QQ00001", "data": "\" OK\ ""}
The above query command can also be rewritten as:
Db.dbtestregex.find ({data:/OK/})
The query result is the same, as follows:
{"_ id": ObjectId ("5d305bb74857fc49c0c14c87"), "order": "QQ00003", "data": "\" OK\ "}, {" _ id ": ObjectId (" 5d305bd14857fc49c0c14c88 ")," order ":" QQ00002 "," data ":"\ "OK\"}, {"_ id": ObjectId ("5d305be94857fc49c0c14c89"), "order": "QQ00001", "data": "\" OK\ ""}
It can also be written as:
Db.dbtestregex.find ({data: {$regex:'OK'}})
It has the same function.
3. Implementation of not like function in Class Relational Database
Although reverse query is not common, it is also necessary for our DBA. Let's do a case study to see how to implement the query of not like function in MongoDB.
Requirements query documents without OK in the data field
The execution script is as follows:
Db.dbtestregex.find ({data: {$not: / OK/}})
The returned document is as follows
{"_ id": ObjectId ("5d305b1c4857fc49c0c14c81"), "order": "QQ00001", "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b3b4857fc49c0c14c82 ")," order ":" QQ00001 "," data ":"\ "ERR repeat order\"}, {"_ id": ObjectId ("5d305b404857fc49c0c14c83"), "order": "QQ00002" "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b454857fc49c0c14c84 ")," order ":" QQ00002 "," data ":"\ "ERR repeat order\"}, {"_ id": ObjectId ("5d305b4b4857fc49c0c14c85"), "order": "QQ00003", "data": "\" ERR repeat order\ "}, {" _ id ": ObjectId (" 5d305b4f4857fc49c0c14c86 ") "order": "QQ00003", "data": "\" ERR repeated orders\ ""} Thank you for reading this article carefully. I hope the article "sample Analysis of MongoDB Fuzzy query Operation" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!