MongoDB $in Operator

MongoDB provides different types of logical query operators, and the $in operator is one of them. The $in operator is used to select documents in which the field's value equals any of the given values in the array. You can use this operator in methods like find(), update(), etc., according to your requirements.

Syntax:

Examples:

In the following examples, we are working with:

Example 1: Matching values using $in operator

In this example, we are only retrieving the data of students whose name is either Jonny or Mia.

Output:

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

Example 2: Matching values in an array using $in operator

In this example, we are only retrieving the data of students working with either JavaScript or CSS.

Output:

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

Example 3: MongoDB Logical $or Operator (retrieving the data in the embedded documents)

In this example, we are only retrieving those student's documents whose City is "London".

Output:

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

Example 4: Updating data using $in operator

In this example, we are adding a new field-value pair (Pending_fees: 88.5) to Mick, Zoya, and Mia's documents by using the update() method with the $in and $set operators.

Note: The update() method updates only one document at a time by default. Therefore, in this example, the update() method will only update the first document that matches the given condition. If you want to update multiple documents, set the value of its multi parameter to true.

Output:

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





Latest Courses