当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


MongoDB $or用法及代码示例


MongoDB 提供了多种逻辑查询操作符。 $or 运算符是这些运算符之一。 $or 运算符对一个或多个表达式的数组执行逻辑 "OR operations"。此运算符仅用于检索与数组中至少一个给定表达式匹配的文档。

$OR 运算符用于在单个查询中查找多个表达式,该查询只需要文档中的一个匹配条件。多个键和值可以与 $or 运算符一起使用。

  1. 用户可以根据需要在 find()、update() 等方法中使用该运算符。
  2. 您还可以将此运算符用于 GeoSpatial 查询、排序操作和文本查询。

用法:

{ $or:[ { Expression 1 }, { Expression 2 }, ..., { Expression N } ] }

例子:

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

Database:JavaTpoint
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":"Zoya",
           "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":"MCA",
           "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":"BA",
           "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:MongoDB 逻辑 $or 运算符:

在此示例中,我们仅检索 Course 为 "MCA" 或 batch_year 为 2018 年的学生的数据。

>db.student.find({$or:[{Course:"MCA"}, {batch_year:2018}]}).pretty()

输出:

>db.sutdent.find({$or:[{Course:"MCA"}, {batch_year:2018}]}).pretty()
{
           "_id":ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name":"Mick",
           "Coruse":"btech",
           "batch_year":2018,
           "language":["c++", "java", "python"],
           "personal_details":
                     {
                      "Father_name":"Jonny",
                      "phone_no":8895321456,
                      "age":23,
                      "gender":"Male",
                      "City":"NewYork",
                     }            
}
{
           "_id":ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name":"Jonny",
           "Coruse":"MCA",
           "batch_year":2019,
           "language":["C#", "java", "PHP"],
           "personal_details":
                     {
                      "Father_name":"Thomas",
                      "phone_no":7845123698,
                      "age":24,
                      "gender":"Male",
                      "City":"London",
                     }          
}

示例 2:MongoDB 逻辑 $or 运算符(检索嵌入文档中的数据):

在此示例中,我们仅检索 City 为 "London" 或年龄为 20 的那些学生的文档。

>db.student.find({$or:[{"personal.age":20}, {"personal.City":"London"}]}).pretty()

输出:

>db.student.find({$or:[{"personal.age":20}, {"personal.City":"London"}]}).pretty()
{
           "_id":ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name":"Zoya",
           "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":"MCA",
           "batch_year":2019,
           "language":["C#", "java", "PHP"],
           "personal_details":
                     {
                      "Father_name":"Thomas",
                      "phone_no":7845123698,
                      "age":24,
                      "gender":"Male",
                      "City":"London",
                     }          
}

示例 3:使用 $or 运算符匹配数组中的值:

在此示例中,我们仅检索与给定数组中至少匹配一个值的那些学生的文档。

>db.student.find({$or:[{language:{$in:["c++", "java", "python"]}}]}).pretty()

输出:

>db.student.find({$or:[{language:{$in:["c++", "java", "HTML"]}}]}).pretty()
{
           "_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("56254d4fdf2222265r4g12ds3d655"),
           "name":"Jonny",
           "Course":"MCA",
           "batch_year":2019,
           "language":["C#", "java", "PHP"],
           "personal_details":
                     {
                      "Father_name":"Thomas",
                      "phone_no":7845123698,
                      "age":24,
                      "gender":"Male",
                      "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",
                     }           
}




相关用法


注:本文由纯净天空筛选整理自 MongoDB $or Operator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。