
- 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 project specific elements in an array field with MongoDB?
To project-specific elements in an array field, use $project. Let us create a collection with documents −
>db.demo355.insertOne({"id":101,"details":[{"Name":"Chris",isMarried:1},{"Name":"David",isMarried:0},{"Name":"Mike",isMarried:1}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e568928f8647eb59e5620c5") }
Display all documents from a collection with the help of find() method −
> db.demo355.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e568928f8647eb59e5620c5"), "id" : 101, "details" : [ { "Name" : "Chris", "isMarried" : 1 }, { "Name" : "David", "isMarried" : 0 }, { "Name" : "Mike", "isMarried" : 1 } ] }
Following is the query to project-specific elements in an array field −
> db.demo355.aggregate([ ... { ... $project: { ... details: { ... $filter: { ... input: "$details", ... as: "out", ... cond: { $eq:["$$out.isMarried",1] } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e568928f8647eb59e5620c5"), "details" : [ { "Name" : "Chris", "isMarried" : 1 }, { "Name" : "Mike", "isMarried" : 1 } ] }
- Related Articles
- Project specific array field in a MongoDB collection?
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- How to project specific fields from a document inside an array in Mongodb?
- Get count of array elements from a specific field in MongoDB documents?
- Delete all elements in an array field in MongoDB?
- How to select objects where an array contains only a specific field in MongoDB?
- Working with MongoDB $concatArrays in $project on existing multi-array field
- Fetch a specific document in MongoDB with array elements
- How to find specific array elements in MongoDB document with query and filter with range?
- Project field in MongoDB
- Replace an array field value with MongoDB?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Find all collections in MongoDB with specific field?
- Get specific elements from embedded array in MongoDB?
- MongoDB aggregation to fetch documents with specific field value?

Advertisements