
- 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
Get all embedded documents with āisMarriedā status in a MongoDB collection
To get all embedded documents, use $project in MongoDB. Let us create a collection with documents −
> db.demo220.insertOne({ ... "id":101, ... "FullName" : "John Doe", ... "EmailId" : "john12@gmail.com", ... "ShippingDate" : new ISODate(), ... "details" : { "_id" :1001, "isMarried" :true } ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3eaf5b03d395bdc213471d") } > db.demo220.insertOne( ...{ ... "id":102, ... "FullName" : "John Smith", ... "EmailId" : "johnsmith@gmail.com", ... "ShippingDate" : new ISODate(), ... "details" : { "_id" :1002, "isMarried" :false } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e3eaf5c03d395bdc213471e") }
Display all documents from a collection with the help of find() method −
> db.demo220.find();
This will produce the following output −
{ "_id" : ObjectId("5e3eaf5b03d395bdc213471d"), "id" : 101, "FullName" : "John Doe", "EmailId" : "john12@gmail.com", "ShippingDate" : ISODate("2020-02-08T12:53:47.876Z"), "details" : { "_id" : 1001, "isMarried" : true } } { "_id" : ObjectId("5e3eaf5c03d395bdc213471e"), "id" : 102, "FullName" : "John Smith", "EmailId" : "johnsmith@gmail.com", "ShippingDate" : ISODate("2020-02-08T12:53:48.991Z"), "details" : { "_id" : 1002, "isMarried" : false } }
Following is the query to get all embedded documents −
> db.demo220.aggregate({ $project : { ... "isMarried" : "$details.isMarried", ... "detailsId" : "$details_id", } });
This will produce the following output −
{ "_id" : ObjectId("5e3eaf5b03d395bdc213471d"), "isMarried" : true } { "_id" : ObjectId("5e3eaf5c03d395bdc213471e"), "isMarried" : false }
- Related Articles
- Get MongoDB documents with max attribute per group in a collection?
- MongoDB - Query embedded documents?
- Get the maximum mark records from a collection with documents in MongoDB
- Check for existing documents/embedded documents in MongoDB
- Get the maximum mark records from a collection with documents in MongoDB query
- Fetching all documents from MongoDB Collection in a beautified form
- Updating Nested Embedded Documents in MongoDB?
- How to delete all the documents from a collection in MongoDB?
- Find all duplicate documents in a MongoDB collection by a key field?
- Get all fields names in a MongoDB collection?
- Appending an entry in one to many embedded documents with MongoDB
- Get the count of the number of documents in a MongoDB Collection?
- Find a value in lowercase from a MongoDB collection with documents
- Display only a single field from all the documents in a MongoDB collection
- Set server status to inactive in a MongoDB collection with server records?

Advertisements