MongoDB 查询文档中使用正则选择器($regex)

之前我们介绍过使用文本选择器($text)查询文档,如果您需要进一步了解,可以参考:

MongoDB 查询文档中使用文本选择器($text)https://blog.csdn.net/m1729339749/article/details/130605491

本篇,我们介绍使用$regex选择器查询文档:

一、准备工作

初始化学生信息数据

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: "我不喜欢健身,我喜欢突破自己" }
])

二、正则选择器($regex)

语法:

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
{ <field>: /pattern/<options> }

上面的三种语法都是用于正则表达式进行检索的。

/pattern/:代表的式正则表达式对象

'pattern':代表的式正则表达式文本

$options:代表的是选项,

        i:不区分大小写

        m:正则表达式中包含了锚点(^代表开始,$代表结尾)

三、例子:检索furits中包含apple的文档

查询文档的命令如下:

db.studentInfos.find({
    fruits: { $regex: /apple/, $options: '' }
})

 等效于:

db.studentInfos.find({
    fruits: { $regex: /apple/ }
})

等效于:

db.studentInfos.find({
    fruits: /apple/
})

查询文档的结果如下:

{ "_id" : 1, "name" : "张三1", "sports" : "羽毛球、乒乓球", "fruits" : "apple, banana", "introduce" : "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" }
{ "_id" : 7, "name" : "张三3", "sports" : "跑步、跳绳", "fruits" : "Orange, apple, Banana", "introduce" : "haha, 与健身相比,我更喜欢吃" }

四、例子:检索furits中包含apple的文档(不区分大小写)

在上面的例子中,我们发现fruits中带有Apple的文档未被检索出来,原因是默认情况下检索的文本是区分大小写的,如果不区分大小写,可使用下面的查询命令:

db.studentInfos.find({
    fruits: { $regex: /apple/, $options: 'i' }
})

等效于:

db.studentInfos.find({
    fruits: { $regex: /apple/i }
})

等效于:

db.studentInfos.find({
    fruits: /apple/i
})

查询文档的结果如下:

{ "_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, 与健身相比,我更喜欢吃" }

五、例子:检索furits中第一个是apple的文档(不区分大小写)

查询文档的命令如下:

db.studentInfos.find({
    fruits: { $regex: /^apple/, $options: 'im' }
})

等效于:

db.studentInfos.find({
    fruits: { $regex: /^apple/im }
})

等效于:

db.studentInfos.find({
    fruits: /^apple/im
})

查询文档的结果如下:

{ "_id" : 1, "name" : "张三1", "sports" : "羽毛球、乒乓球", "fruits" : "apple, banana", "introduce" : "大家好,我喜欢运动,但是我不喜欢铅球,因为它太重了" }
{ "_id" : 2, "name" : "李四1", "sports" : "篮球、乒乓球、跑步", "fruits" : "Apple", "introduce" : "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" }

六、例子:检索furits中最后一个是apple的文档(不区分大小写)

查询文档的命令如下:

db.studentInfos.find({
    fruits: { $regex: /apple$/, $options: 'im' }
})

等效于:

db.studentInfos.find({
    fruits: { $regex: /apple$/im }
})

等效于:

db.studentInfos.find({
    fruits: /apple$/im
})

查询文档的结果如下:

{ "_id" : 2, "name" : "李四1", "sports" : "篮球、乒乓球、跑步", "fruits" : "Apple", "introduce" : "大家好,我喜欢水果,但是不喜欢吃橘子,因为它太酸了" }
{ "_id" : 5, "name" : "李四2", "sports" : "乒乓球", "fruits" : "Orange, Apple", "introduce" : "大家好,我喜欢乒乓球运动" }

猜你喜欢

转载自blog.csdn.net/m1729339749/article/details/130671326
今日推荐