
- 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
Retrieving array values from a find query in MongoDB database
To retrieve array values from a find query, use dot notation. Let us create a collection with documents −
> db.demo38.insertOne({"ClientDetails":[{"ClientId":101,"ClientName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176abccfb11e5c34d898d9") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":102,"ClientName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ac7cfb11e5c34d898da") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":103,"ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ad0cfb11e5c34d898db") }
Display all documents from a collection with the help of find() method −
> db.demo38.find();
This will produce the following output −
{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] } { "_id" : ObjectId("5e176ad0cfb11e5c34d898db"), "ClientDetails" : [ { "ClientId" : 103, "ClientName" : "Mike" } ] }
Following is the query to retrieve array values from a find query −
> db.demo38.find({"ClientDetails.ClientName":"David"});
This will produce the following output −
{ "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] }
- Related Articles
- Retrieving array values from a find query in MongoDB?
- MongoDB query to pull multiple values from array
- MongoDB query to fetch array values
- Retrieving a Subset of Fields from MongoDB
- Retrieving MySQL Database structure information from Java?
- MongoDB query to find matching documents given an array with values?
- MongoDB query to remove array elements from a document?
- MongoDB query to pull array element from a collection?
- MongoDB query to find data from an array inside an object?
- MongoDB query to remove item from array?
- Retrieving specific documents from collection by _id in MongoDB
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Getting distinct values from object array in MongoDB?
- Retrieve values from nested JSON array in MongoDB?
- MongoDB query to get distinct FirstName values from documents

Advertisements