We have introduced the use of text selectors ($text) to query documents before, if you need to know more, you can refer to:
In this article, we introduce the use of the $regex selector to query documents:
1. Preparation
Initialize student information data
db.studentInfos.insertMany([
{ "_id": 1, "name": "张三1", "sports": "羽毛球、乒乓球", fruits: "apple, banana", introduce: "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" },
{ "_id": 2, "name": "李四1", "sports": "篮球、乒乓球、跑步", fruits: "Apple", introduce: "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" },
{ "_id": 3, "name": "王五1", "sports": "足球、羽毛球", fruits: "Banana", introduce: "大家好,我喜欢水果,喜欢运动" },
{ "_id": 4, "name": "张三2", "sports": "网球、铅球", fruits: "orange", introduce: "大家好,我喜欢水果与健身" },
{ "_id": 5, "name": "李四2", "sports": "乒乓球", fruits: "Orange, Apple", introduce: "大家好,我喜欢乒乓球运动" },
{ "_id": 6, "name": "王五2", "sports": "篮球", fruits: "Orange, Banana", introduce: "hello,i like sports" },
{ "_id": 7, "name": "张三3", "sports": "跑步、跳绳", fruits: "Orange, apple, Banana", introduce: "haha, 与健身相比,我更喜欢吃" },
{ "_id": 8, "name": "李四3", "sports": "足球", fruits: "orange, banana", introduce: "足球和橘子是我的最爱" },
{ "_id": 9, "name": "王五3", "sports": "跆拳道", fruits: "Banana", introduce: "我不喜欢健身,我喜欢突破自己" }
])
2. Regular selector ($regex)
grammar:
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
{ <field>: /pattern/<options> }
The above three syntaxes are all used for regular expression retrieval.
/pattern/: represents the pattern regular expression object
'pattern': the pattern representing the regular expression text
$options: represents options,
i: case insensitive
m: Anchor points are included in the regular expression (^ represents the beginning, $ represents the end)
3. Example: Retrieve documents containing apple in furits
The command to query the document is as follows:
db.studentInfos.find({
fruits: { $regex: /apple/, $options: '' }
})
Equivalent to:
db.studentInfos.find({
fruits: { $regex: /apple/ }
})
Equivalent to:
db.studentInfos.find({
fruits: /apple/
})
The results of the query document are as follows:
{ "_id" : 1, "name" : "张三1", "sports" : "羽毛球、乒乓球", "fruits" : "apple, banana", "introduce" : "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" }
{ "_id" : 7, "name" : "张三3", "sports" : "跑步、跳绳", "fruits" : "Orange, apple, Banana", "introduce" : "haha, 与健身相比,我更喜欢吃" }
4. Example: Retrieve documents containing apple in furits (case insensitive)
In the above example, we found that the documents with Apple in fruits were not retrieved, because the retrieved text is case-sensitive by default. If it is not case-sensitive, you can use the following query command:
db.studentInfos.find({
fruits: { $regex: /apple/, $options: 'i' }
})
Equivalent to:
db.studentInfos.find({
fruits: { $regex: /apple/i }
})
Equivalent to:
db.studentInfos.find({
fruits: /apple/i
})
The results of the query document are as follows:
{ "_id" : 1, "name" : "张三1", "sports" : "羽毛球、乒乓球", "fruits" : "apple, banana", "introduce" : "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" }
{ "_id" : 2, "name" : "李四1", "sports" : "篮球、乒乓球、跑步", "fruits" : "Apple", "introduce" : "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" }
{ "_id" : 5, "name" : "李四2", "sports" : "乒乓球", "fruits" : "Orange, Apple", "introduce" : "大家好,我喜欢乒乓球运动" }
{ "_id" : 7, "name" : "张三3", "sports" : "跑步、跳绳", "fruits" : "Orange, apple, Banana", "introduce" : "haha, 与健身相比,我更喜欢吃" }
5. Example: Retrieve the first document of apple in furits (case insensitive)
The command to query the document is as follows:
db.studentInfos.find({
fruits: { $regex: /^apple/, $options: 'im' }
})
Equivalent to:
db.studentInfos.find({
fruits: { $regex: /^apple/im }
})
Equivalent to:
db.studentInfos.find({
fruits: /^apple/im
})
The results of the query document are as follows:
{ "_id" : 1, "name" : "张三1", "sports" : "羽毛球、乒乓球", "fruits" : "apple, banana", "introduce" : "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" }
{ "_id" : 2, "name" : "李四1", "sports" : "篮球、乒乓球、跑步", "fruits" : "Apple", "introduce" : "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" }
6. Example: Retrieve the document that the last one in furits is apple (case insensitive)
The command to query the document is as follows:
db.studentInfos.find({
fruits: { $regex: /apple$/, $options: 'im' }
})
Equivalent to:
db.studentInfos.find({
fruits: { $regex: /apple$/im }
})
Equivalent to:
db.studentInfos.find({
fruits: /apple$/im
})
The results of the query document are as follows:
{ "_id" : 2, "name" : "李四1", "sports" : "篮球、乒乓球、跑步", "fruits" : "Apple", "introduce" : "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" }
{ "_id" : 5, "name" : "李四2", "sports" : "乒乓球", "fruits" : "Orange, Apple", "introduce" : "大家好,我喜欢乒乓球运动" }