
- 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
Can we search an array of objects in MongoDB?
Yes, to search an array of objects, use $unwind in MongoDB aggregate(). To match, use $match. Let us create a collection with documents −
> db.demo623.insertOne( ... { ... _id:1, ... details:[ ... { ... Name:"Chris" ... }, ... { ... DueDate:new ISODate("2020-01-10") ... }, ... { ... CountryName:"US" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo623.find().pretty();
This will produce the following output −
{ "_id" : 1, "details" : [ { "Name" : "Chris" }, { "DueDate" : ISODate("2020-01-10T00:00:00Z") }, { "CountryName" : "US" } ] }
Following is the query to search an array of objects in MongoDB −
> db.demo623.aggregate({$unwind: "$details"}, ... {$match: {"details.Name":"Chris"}}, ... {$project: {"details.Name": 1}})
This will produce the following output −
{ "_id" : 1, "details" : { "Name" : "Chris" } }
- Related Articles
- Search array of objects in a MongoDB collection?
- Can we store objects in an array in Java?
- Search an array of hashes in MongoDB?
- Search from an array of objects via array of string to get array of objects in JavaScript
- How can we serialize an array of objects using flexjson in Java?
- Can we share a method between JavaScript objects in an array?
- Update an array of strings nested within an array of objects in MongoDB
- Pull multiple objects from an array in MongoDB?
- How can we make an Array of Objects from n properties of n arrays in JavaScript?
- Find by _id on an array of objects in MongoDB database?
- How to search documents through an integer array in MongoDB?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- MongoDB query to remove empty objects in an object-array?
- Can we pass objects as an argument in Java?
- Search for documents matching first item in an array with MongoDB?

Advertisements