
- 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
Project specific array field in a MongoDB collection?
Let us first create a collection with documents −
> db.projectionAnElementDemo.insertOne( ... { ... "CustomerId":100, ... "CustomerDetails": [ ... { ... "CustomerName": "Chris", ... "CustomerCountryName": "US" ... }, ... { ... "CustomerName": "Robert", ... "CustomerCountryName": "UK" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd31c56b64f4b851c3a13ea") }
Following is the query to display all documents from a collection with the help of find() method −
> db.projectionAnElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd31c56b64f4b851c3a13ea"), "CustomerId" : 100, "CustomerDetails" : [ { "CustomerName" : "Chris", "CustomerCountryName" : "US" }, { "CustomerName" : "Robert", "CustomerCountryName" : "UK" } ] }
Following is the query to project element in array field −
> db.projectionAnElementDemo.find({},{CustomerId:1, "CustomerDetails.CustomerName":1}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd31c56b64f4b851c3a13ea"), "CustomerId" : 100, "CustomerDetails" : [ { "CustomerName" : "Chris" }, { "CustomerName" : "Robert" } ] }
- Related Articles
- How to project specific elements in an array field with MongoDB?
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- Project field in MongoDB
- Accessing array values in a MongoDB collection to fetch a specific document
- Return a specific field in MongoDB?
- MongoDB Aggregate JSON array field for the matching field of other collection?
- Working with MongoDB $concatArrays in $project on existing multi-array field
- 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?
- How to check empty field in a MongoDB collection?
- Search by specific field in MongoDB
- Fetch specific field values in MongoDB
- How to select objects where an array contains only a specific field in MongoDB?
- Add new field to every document in a MongoDB collection?
- MongoDB query to update a specific document from a collection

Advertisements