MongoDB $in 运算符

MongoDB $in 运算符

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

语法

{ field: { $in: [<value 1>, <value 2>, ... <value 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:使用$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操作符更新数据

在这个示例中,我们使用update()方法和in和set操作符,将一个新的字段-值对(Pending_fees: 88.5)添加到Mick、Zoya和Mia的文档中。

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

注意:默认情况下,update()方法一次只能更新一个文档。因此,在这个示例中,update()方法只会更新与给定条件匹配的第一个文档。如果你想要更新多个文档,请将其multi参数的值设为true。

输出:

>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
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程