
- 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 fetch array values
Use find() along with $elemMatch to fetch array values. Let us first create a collection with documents −
> db.fetchingArrayValuesDemo.insertOne( ... { ... "StudentName": "David", ... "StudentDetails": [ ... { ... "FatherName": "Bob", ... "CountryName": "US", ... ... "Favourite": [ ... { ... "Teacher": "DAVID", ... "Subject": [ ... "MySQL", ... "MongoDB", ... "Java" ... ], ... "Marks": [ ... 50, ... 60, ... 65 ... ] ... } ... ] ... ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e06fc3425ddae1f53b621fa") } > db.fetchingArrayValuesDemo.insertOne( ... { ... "StudentName": "Robert", ... "StudentDetails": [ ... { ... "FatherName": "Sam", ... "CountryName": "AUS", ... ... "Favourite": [ ... { ... "Teacher": "MIKE", ... "Subject": [ ... "Python", ... "C", ... "C++" ... ], ... "Marks": [ ... 76, ... 89, ... 91 ... ] ... } ... ] ... ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e06fc6825ddae1f53b621fb") }
Following is the query to display all documents from a collection with the help of find() method −
> db.fetchingArrayValuesDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e06fc3425ddae1f53b621fa"), "StudentName" : "David", "StudentDetails" : [ { "FatherName" : "Bob", "CountryName" : "US", "Favourite" : [ { "Teacher" : "DAVID", "Subject" : [ "MySQL", "MongoDB", "Java" ], "Marks" : [ 50, 60, 65 ] } ] } ] } { "_id" : ObjectId("5e06fc6825ddae1f53b621fb"), "StudentName" : "Robert", "StudentDetails" : [ { "FatherName" : "Sam", "CountryName" : "AUS", "Favourite" : [ { "Teacher" : "MIKE", "Subject" : [ "Python", "C", "C++" ], "Marks" : [ 76, 89, 91 ] } ] } ] }
Here is the query to fetch array values −
> db.fetchingArrayValuesDemo.find({ ... StudentDetails:{ ... $elemMatch: { ... Favourite: { ... $elemMatch: { ... Teacher: "DAVID" ... } ... } ... } ... } ... });
This will produce the following output −
{ "_id" : ObjectId("5e06fc3425ddae1f53b621fa"), "StudentName" : "David", "StudentDetails" : [ { "FatherName" : "Bob", "CountryName" : "US", "Favourite" : [ { "Teacher" : "DAVID", "Subject" : [ "MySQL", "MongoDB", "Java" ], "Marks" : [ 50, 60, 65 ] } ] } ] }
- Related Articles
- Query an array in MongoDB to fetch a specific value
- MongoDB query to pull multiple values from array
- Array index or indexing inner items in MongoDB to fetch values
- MySQL query to fetch multiple least values?
- Accessing array values in a MongoDB collection to fetch a specific document
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MySQL query to fetch specific records matched from an array (comma separated values)
- MongoDB query to concatenate values of array with other fields
- Fetch specific field values in MongoDB
- MongoDB query to implement nor query to fetch documents except a specific document
- Retrieving array values from a find query in MongoDB?
- MongoDB query to find matching documents given an array with values?
- MongoDB query to fetch random value using Map Reduce concept.
- Retrieving array values from a find query in MongoDB database
- MongoDB query to fetch date records (ISODate format) in a range

Advertisements