
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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
- Related Articles
- Search an array of hashes in MongoDB?
- How to search documents through an integer array in MongoDB?
- How to remove object from array in MongoDB?
- Search array of objects in a MongoDB collection?
- How to compare attributes of different objects in MongoDB object array?
- Can we search an array of objects in MongoDB?
- How to get items from an object array in MongoDB?
- Remove object from array in MongoDB?
- How to push new items to an array inside of an object in MongoDB?
- How to search in a List of Java object?
- Search elements in a sorted object array in Java
- How to search document in MongoDB by _id
- Increment value of an array element with array object in MongoDB
- Perform $lookup to array of object id's in MongoDB?
- MongoDB query to access an object in an array

Advertisements