
- 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 a specific field in array using $project in MongoDB and ignore other fields?
To display a specific field, use $project along with $unwind. To ignore a field, set to 0. Let us create a collection with documents −
> db.demo731.insertOne({ "ProductInformation": [ { ProductId:"Product-1", ProductPrice:80 }, { ProductId:"Product-2", ProductPrice:45 }, { ProductId:"Product-3", ProductPrice:50 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5eac5efd56e85a39df5f6341") }
Display all documents from a collection with the help of find() method −
> db.demo731.find();
This will produce the following output −
{ "_id" : ObjectId("5eac5efd56e85a39df5f6341"), "ProductInformation" : [ { "ProductId" : "Product-1", "ProductPrice" : 80 }, { "ProductId" : "Product-2", "ProductPrice" : 45 }, { "ProductId" : "Product-3", "ProductPrice" : 50 } ] }
Following is the query to display a specific field in array using $project in MongoDB −
> db.demo731.aggregate([ ... { $unwind: "$ProductInformation" }, ... { $match: { "ProductInformation.ProductPrice": 80} }, ... { $project: {_id: 0,"ProductInformation.ProductPrice":0}} ... ])
This will produce the following output −
{ "ProductInformation" : { "ProductId" : "Product-1" } }
- Related Articles
- Project specific array field in a MongoDB collection?
- How to project specific fields from a document inside an array in Mongodb?
- How to project specific elements in an array field with MongoDB?
- Sort array in MongoDB query and project all fields?
- Regex to ignore a specific character in MongoDB?
- How to project grouping into object in MongoDB and display only the marks field?
- MongoDB query to get only specific fields in nested array documents?
- Project field in MongoDB
- MongoDB query to return specific fields from an array?
- How to select objects where an array contains only a specific field in MongoDB?
- MongoDB query select and display only a specific field from the document?
- Update only specific fields in MongoDB?
- Return a specific field in MongoDB?
- MongoDB query to concatenate values of array with other fields
- MongoDB query to sum specific fields

Advertisements