MongoDB $in 运算符

MongoDB $in 运算符 介绍

MongoDB 提供了不同类型的逻辑查询运算符,$in 运算符就是其中之一。$in 运算符用于选择字段值等于数组中任何给定值的文档。您可以根据需要在 find()、update() 等方法中使用此运算符。

MongoDB $in 运算符 语法

{ field: { $ in : [<value 1>, <value 2>, ... <value N> ] } }  

MongoDB $in 运算符 例子

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

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:使用 $in 运算符匹配值

在此示例中,我们仅检索姓名为 Jonny 或 Mia 的学生的数据。

>db.student.find({ name : {$ in : [ "Jonny" ,  "Mia" ]}}).pretty()  

输出结果为:

>db.student.find({name: {$in: ["Jonny", "Mia"]}}).pretty()
{
           "_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",
                     }           
}

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

在此示例中,我们仅检索使用JavaScript或CSS的学生的数据。

>db.student.find({language: {$ in : [ "JavaScript" ,  "CSS" ]}}).pretty()  

输出结果为:

>db.student.find({language: {$in: ["JavaScript", "CSS"]}}).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("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:MongoDB 逻辑 $or 运算符(检索嵌入文档中的数据)

在此示例中,我们仅检索城市为“伦敦”的学生文档。

>db.student.find({ "personal.City" : {$ in : [ "London" ]}}).pretty()  

输出结果为:

>db.student.find({"personal.City": {$in: ["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",
                     }          
}

示例 4:使用 $in 运算符更新数据

在此示例中,我们使用带有 $in 和 $set 运算符的 update() 方法向 Mick、Zoya 和 Mia 的文档添加一个新的字段值对 (Pending_fees: 88.5)。

>db.student.update({name: {$in: ["Mick", "Zoya", "Mia"]}}, {$set: {Pending_fees: 12000}})  

输出结果为:

>db.student.update({name: {$in: ["Mick", "Zoya", "Mia"]}}, {$set: {Pending_fees: 12000}})
{
           "_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",
                     }            
            "Pending_fees" : 12000
}

 

热门文章

优秀文章