
- 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
Find in MongoDB documents with filled nested array and reshape the documents result
Let us first create a collection with documents −
> db.demo187.insertOne( ... { ... "_id" : "101", ... "Details" : [ ... { "Subject" : "MongoDB" }, ... { "Subject" : "MySQL" } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" } > db.demo187.insertOne( ... { ... "_id" : "102", ... "Details" : [ ... { } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "102" } > db.demo187.insertOne( ... { ... "_id" : "103", ... "Details" : [ ... { "Subject" : "MongoDB" }, ... { "Subject" : "MySQL" } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "103" }
Display all documents from a collection with the help of find() method −
> db.demo187.find();
This will produce the following output −
{ "_id" : "101", "Details" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] } { "_id" : "102", "Details" : [ { } ] } { "_id" : "103", "Details" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] }
Following is the query to find in MongoDB documents with filled nested array −
> db.demo187.aggregate([ ... ... {$unwind: '$Details'}, ... ... {$project: {Subject: '$Details.Subject'}}, ... ... {$match: {Subject: {$exists: true}}} ... ])
This will produce the following output −
{ "_id" : "101", "Subject" : "MongoDB" } { "_id" : "101", "Subject" : "MySQL" } { "_id" : "103", "Subject" : "MongoDB" } { "_id" : "103", "Subject" : "MySQL" }
- Related Articles
- Update objects in a MongoDB documents array (nested updating)?
- Aggregation in MongoDB for nested documents?
- Updating Nested Embedded Documents in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- MongoDB query to get only specific fields in nested array documents?
- How can I aggregate nested documents in MongoDB?\n
- Find current and previous documents in MongoDB
- MongoDB query to find matching documents given an array with values?
- How to display only the keys from nested MongoDB documents?
- How to aggregate array documents in MongoDB?
- Match MongoDB documents with fields not containing values in array?
- MongoDB query to find documents with specific FirstName and LastName
- Check for existing documents/embedded documents in MongoDB
- Match ID and fetch documents with $eq in MongoDB in case of array?
- How to find documents with exactly the same array entries as in a MongoDB query?

Advertisements