How to search in array of object in MongoDB?


To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Adam","EmployeeLastName":"Smith","EmployeeDateOfBirth":new ISODate("1992-01-31 13:45:10"),
   ... "EmployeeSkills":["Spring and Hibernate Framework","Machine Learning"],
   ... "EmployeeDetails":[
      ... {
         ... "EmployeePerformanceArea":"Java",
         ... "Year":2001
      ... },
      ... {
      ... "EmployeePerformanceArea":"Python",
         ... "Year":2005
      ... }
   ... ]
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c77b3812386c62d05142a6a")
}
> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Carol","EmployeeLastName":"Taylor",
   "EmployeeDateOfBirth":new ISODate("1993-04-21 11:10:20"),
   "EmployeeSkills":["C++","Cloud Computing"], "EmployeeDetails":[ {
   "EmployeePerformanceArea":"C++", "Year":1998 }, { "EmployeePerformanceArea":"C++
   Game Developer", "Year":2007 } ] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c77b58f2386c62d05142a6b")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.searchArrayDemo.find().pretty();

The following is the output −

{
   "_id" : ObjectId("5c77b3812386c62d05142a6a"),
   "EmployeeFirstName" : "Adam",
   "EmployeeLastName" : "Smith",
   "EmployeeDateOfBirth" : ISODate("1992-01-31T13:45:10Z"),
   "EmployeeSkills" : [
      "Spring and Hibernate Framework",
      "Machine Learning"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "Java",
         "Year" : 2001
      },
      {
         "EmployeePerformanceArea" : "Python",
         "Year" : 2005
      }
   ]
}
{
   "_id" : ObjectId("5c77b58f2386c62d05142a6b"),
   "EmployeeFirstName" : "Carol",
   "EmployeeLastName" : "Taylor",
   "EmployeeDateOfBirth" : ISODate("1993-04-21T11:10:20Z"),
   "EmployeeSkills" : [
      "C++",
      "Cloud Computing"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "C++",
         "Year" : 1998
      },
      {
         "EmployeePerformanceArea" : "C++ Game Developer",
         "Year" : 2007
      }
   ]
}

Here is the query to search in an array of objects in MongoDB.

Case 1

When the given element is found.

The query is as follows −

> db.searchArrayDemo.find({EmployeeDetails:{$elemMatch:{EmployeePerformanceArea : "C++", Year : 1998}}}).pretty();

The following is the output −

{
   "_id" : ObjectId("5c77b58f2386c62d05142a6b"),
   "EmployeeFirstName" : "Carol",
   "EmployeeLastName" : "Taylor",
   "EmployeeDateOfBirth" : ISODate("1993-04-21T11:10:20Z"),
   "EmployeeSkills" : [
      "C++",
      "Cloud Computing"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "C++",
         "Year" : 1998
      },
      {
         "EmployeePerformanceArea" : "C++ Game Developer",
         "Year" : 2007
      }
   ]
}

Case 2

When the given element is not found.

The query is as follows −

> db.searchArrayDemo.find({EmployeeDetails:{$elemMatch:{EmployeePerformanceArea : "C", Year : 1996}}}).pretty();

Here you will not get anything when the given element is not found

Updated on: 10-Sep-2023

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements