MongoDB 正则表达式

在 MongoDB 中,正则表达式用于模式匹配,基本上是在文档中的字符串中搜索模式。它是一种将模式与字符序列匹配的通用方法。$regex 运算符用作正则表达式来查找字符串中的模式。

在 MongoDB 中,我们可以通过两种不同的方式进行模式匹配:

  1. 使用 $regex 运算符进行模式匹配
  2. 没有 $regex 运算符的模式匹配

使用 $regex 运算符进行模式匹配

在 MongoDB 中,$regex 运算符提供了查询中模式匹配的功能。换句话说,$regex 运算符用于搜索文档中的特定字符串。

语法:

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

$options:

以下 <option> 可与正则表达式一起使用:

  1. “i”字符用于匹配字符串中的小写和大写模式。
  2. “^”和“$”符号用于搜索文档中的特定模式。“^”符号用于确保字符串以某个字符开头,$ 用于确保字符串以某个字符结尾。
  3. “x”字符用于忽略字符串中的所有空白字符。

例子:

在以下示例中,我们正在使用:

Database: Yiidian
Collection: student  
Document: Five documents that contain the details of the students  
>db.student.find()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name" : "Jonny",
           "Course" : "MCALE",
           "batch_year" : 2019,
           "language" : ["C#", "java", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Thomas",
                      "phone_no" : 7845123698,
                      "age" : 24,
                      "gender" : "Male",
                      "City" : "London",
                     }          
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "MCA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

示例 1:显示正在攻读 B.Tech 的学生的详细信息。

我们正在显示课程为 btech 的学生的详细信息,因此我们使用 $regex 运算符(即 {$regex : "btech"})为 find() 方法中的课程字段传递一个正则表达式。

>db.student.find({Course : {$regex:  "btech"  }}).forEach(printjson)  

输出结果为:

>db.student.find({Course : {$regex: "btech" }}).forEach(printjson)
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

示例 2:使用 i <options> 显示姓名以“Mi”开头的学生的详细信息。

>db.student.find({ name :{$regex:  "Mi" ,$options: 'i' }}).forEach(printjson)  

输出结果为:

>db.student.find({name:{$regex: "Mi",$options:'i'}}).forEach(printjson)
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

示例 3:显示课程为 MCA 的学生的详细信息。

>db.student.find({Course : {$regex: "^MCA$"}}).forEach(printjson)  

输出结果为:

>db.student.find({Course : {$regex: "^MCA$"}}).forEach(printjson) 
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "MCA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}

没有 $regex 运算符的模式匹配

在 MongoDB 中,我们可以在不使用 $regex 运算符的情况下进行模式匹配。在此方法中,它使用正则表达式对象。

语法:

{ <field>: /pattern/<options> }  

示例 4:使用正则表达式对象显示姓名中包含“ck”的学生的详细信息。

>db.student.find({ name : /ck/}).forEach(printjson)  

“//”表示在这些分隔符中指定您的搜索条件。因此,我们正在寻找名称中包含“CK”的文档。

输出结果为:

{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}

 

热门文章

优秀文章