MongoDB $or运算符
MongoDB提供了多种逻辑查询运算符。$or
运算符是其中之一。$or
运算符对一个或多个表达式的数组执行逻辑“或操作”。该运算符仅用于检索与数组中的至少一个给定表达式匹配的文档。
$OR
运算符用于在单个查询中找到多个表达式,该查询仅需要文档中的一个匹配准则。$or
运算符可以与多个键和值一起使用。
- 用户可以根据自己的需求在find()、update()等方法中使用该运算符。
- 您还可以将此运算符与地理空间查询、排序操作和文本查询一起使用。
语法
{ $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运算符:
在本例中,我们只检索课程为”MCA”或批次为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运算符(检索嵌入文档中的数据):
在此示例中,我们只检索那些城市为”伦敦”或年龄为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",
}
}