
- 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
MongoDB query to return only specific fields (phone numbers) in the form of an array?
Let us create a collection with documents −
> db.demo166.insertOne({"details" : { "UserName" : "Chris", "UserAge":29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } }); { "acknowledged" : true, "insertedId" : ObjectId("5e368b159e4f06af551997cf") } > db.demo166.insertOne({"details" : { "UserName" : "David", "UserAge":21, "PhoneDetails" : { "PhoneNumber" : "87664534654" } } }); { "acknowledged" : true, "insertedId" : ObjectId("5e368b159e4f06af551997d0") }
Display all documents from a collection with the help of find() method −
> db.demo166.find();
This will produce the following output −
{ "_id" : ObjectId("5e368b159e4f06af551997cf"), "details" : { "UserName" : "Chris", "UserAge" : 29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } } { "_id" : ObjectId("5e368b159e4f06af551997d0"), "details" : { "UserName" : "David", "UserAge" : 21, "PhoneDetails" : { "PhoneNumber" : "87664534654" } } }
Following is the query to return only specific fields (phone numbers) in the form of an array −
> db.demo166.distinct("details.PhoneDetails.PhoneNumber");
This will produce the following output −
[ "98646463533", "87664534654" ]
- Related Articles
- MongoDB query to return specific fields from an array?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to sum specific fields
- Update only specific fields in MongoDB?
- MongoDB query to update only certain fields?
- MongoDB query to return only embedded document?
- Query an array in MongoDB to fetch a specific value
- How to query a document in MongoDB comparing fields from an array?
- How to project specific fields from a document inside an array in Mongodb?
- MongoDB query to get only a specific number of elements
- MongoDB query to concatenate values of array with other fields
- MongoDB inverse of query to return all items except specific documents?
- Sort array in MongoDB query and project all fields?
- MongoDB Query to search for records only in a specific hour?
- MongoDB query to slice only one element of array

Advertisements