
- 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?
To retrieve array values, use dot(.) notation. Let us first create a collection with documents −
> db.retrievingArrayDemo.insertOne( { "UserDetails" : [ { "UserName" : "John", "UserAge" : 23 } ], "UserCountryName" : "AUS", "UserLoginDate" : new ISODate(), "UserMessage" : "Hello" } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9718478f00858fb12e920") } > db.retrievingArrayDemo.insertOne( { "UserDetails" : [ { "UserName" : "Sam", "UserAge" : 24 } ], "UserCountryName" : "UK", "UserLoginDate" : new ISODate(), "UserMessage" : "Bye" } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9718478f00858fb12e921") }
Following is the query to display all documents from a collection with the help of find() method −
> db.retrievingArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce9718478f00858fb12e920"), "UserDetails" : [ { "UserName" : "John", "UserAge" : 23 } ], "UserCountryName" : "AUS", "UserLoginDate" : ISODate("2019-05-25T16:47:00.211Z"), "UserMessage" : "Hello" } { "_id" : ObjectId("5ce9718478f00858fb12e921"), "UserDetails" : [ { "UserName" : "Sam", "UserAge" : 24 } ], "UserCountryName" : "UK", "UserLoginDate" : ISODate("2019-05-25T16:47:00.670Z"), "UserMessage" : "Bye" }
Following is the query to retrieve array values from a find query −
> db.retrievingArrayDemo.find({"UserCountryName" : "UK", "UserDetails.UserName":"Sam"}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce9718478f00858fb12e921"), "UserDetails" : [ { "UserName" : "Sam", "UserAge" : 24 } ], "UserCountryName" : "UK", "UserLoginDate" : ISODate("2019-05-25T16:47:00.670Z"), "UserMessage" : "Bye" }
- Related Articles
- Retrieving array values from a find query in MongoDB database
- MongoDB query to pull multiple values from array
- MongoDB query to fetch array values
- Retrieving a Subset of Fields from MongoDB
- MongoDB query to find matching documents given an array with values?
- MongoDB query to remove item from array?
- MongoDB query to pull array element from a collection?
- MongoDB query to remove array elements from a document?
- MongoDB query to find data from an array inside an object?
- Retrieving specific documents from collection by _id in MongoDB
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Retrieve values from nested JSON array in MongoDB?
- Getting distinct values from object array in MongoDB?
- MongoDB query to concatenate values of array with other fields
- MongoDB query to get distinct FirstName values from documents

Advertisements