 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }Advertisements
 