
- 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 display only the keys from nested MongoDB documents?
Let us create a collection with documents −
> db.demo740.insertOne({ ... "details": ... [ ... { ... Name:"Chris", ... Age:21, ... CountryName:"US" ... }, ... { ... Name:"Bob", ... Age:20, ... CountryName:"UK", ... isMarried:true ... } ... ] ... }); { "acknowledged" : true, "insertedId" : ObjectId("5ead700c57bb72a10bcf066d") }
Display all documents from a collection with the help of find() method −
> db.demo740.find();
This will produce the following output −
"_id" : ObjectId("5ead700c57bb72a10bcf066d"), "details" : [ { "Name" : "Chris", "Age" : 21, "CountryName" : "US" }, { "Name" : "Bob", "Age" : 20, "CountryName" : "UK", "isMarried" : true } ] }
Following is the query to get keys of nested MongoDB documents −
> db.demo740.aggregate([ ... { ... $project: { ... ListKeys: { ... $reduce: { ... input: "$details", ... initialValue: [], ... in: { ... $concatArrays: [ ... "$$value", ... { ... $map: { ... input: { ... $objectToArray: "$$this" ... }, ... in: "$$this.k" ... } ... } ... ] ... } ... } ... } ... } ... } ... ]).pretty()
This will produce the following output −
{ "_id" : ObjectId("5ead700c57bb72a10bcf066d"), "ListKeys" : [ "Name", "Age", "CountryName", "Name", "Age", "CountryName", "isMarried" ] }
- Related Articles
- Restrict returned data in MongoDB to display only specific values from documents
- MongoDB query to get only specific fields in nested array documents?
- Display only a single field from all the documents in a MongoDB collection
- Aggregation in MongoDB for nested documents?
- Updating Nested Embedded Documents in MongoDB?
- Aggregation: group date in nested documents (nested object) and display the count?
- How can I sort documents in MongoDB 4 and display only a single field?
- How can I aggregate nested documents in MongoDB?\n
- Using find() to search for nested keys in MongoDB?
- Display only the employee names with specific salaries in MongoDB documents with employee records?
- Find in MongoDB documents with filled nested array and reshape the documents result
- How to only get the data of the nested JSON object in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- How can I display only unique record from MongoDB and ignore the duplicates?
- Filter documents in MongoDB if all keys exist as fields?

Advertisements