MongoDB $or 运算符
MongoDB $or 运算符 介绍
MongoDB 提供了多种逻辑查询运算符。$or 运算符就是这些运算符之一。$or 运算符对一个或多个表达式的数组执行逻辑“或”运算。此运算符仅用于检索与数组中至少一个给定表达式匹配的文档。
$OR 运算符用于在单个查询中查找多个表达式,该查询仅需要文档中的一个匹配条件。$or 运算符可以使用多个键和值。
- 用户可以根据需要在 find()、update() 等方法中使用此运算符。
- 您还可以将此运算符用于地理空间查询、排序操作和文本查询。
MongoDB $or 运算符 语法
{ $or: [ { Expression 1 }, { Expression 2 }, ..., { Expression N } ] }
MongoDB $or 运算符 例子
在以下示例中,我们正在使用:
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" : "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”或 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 运算符(检索嵌入文档中的数据):
在此示例中,我们仅检索城市为“伦敦”或年龄为 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",
}
}
热门文章
优秀文章