
- 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
Accessing array values in a MongoDB collection to fetch a specific document
To access array values, use dot(.) notation. Let us create a collection with documents −
> db.demo326.insertOne({id:101,"ProductDetails":[{"ProductId":"Prod-101","ProductName":"Product-1"}, ... {"ProductId":"Prod-102","ProductName":"Product-2"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e516751f8647eb59e562074") } > db.demo326.insertOne({id:103,"ProductDetails":[{"ProductId":"Prod-104","ProductName":"Product-5"}, ... {"ProductId":"Prod-105","ProductName":"Product-6"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e516771f8647eb59e562075") }
Display all documents from a collection with the help of find() method −
> db.demo326.find();
This will produce the following output −
{ "_id" : ObjectId("5e516751f8647eb59e562074"), "id" : 101, "ProductDetails" : [ { "ProductId" : "Prod-101", "ProductName" : "Product-1" }, { "ProductId" : "Prod-102", "ProductName" : "Product-2" } ] } { "_id" : ObjectId("5e516771f8647eb59e562075"), "id" : 103, "ProductDetails" : [ { "ProductId" : "Prod-104", "ProductName" : "Product-5" }, { "ProductId" : "Prod-105", "ProductName" : "Product-6" } ] }
Following is the query to access the array in a MongoDB collection and fetch a specific document −
> db.demo326.find({"ProductDetails.ProductId":"Prod-104"});
This will produce the following output −
{ "_id" : ObjectId("5e516771f8647eb59e562075"), "id" : 103, "ProductDetails" : [ { "ProductId" : "Prod-104", "ProductName" : "Product-5" }, { "ProductId" : "Prod-105", "ProductName" : "Product-6" } ] }
- Related Articles
- Fetch a specific document in MongoDB with array elements
- MongoDB query to update a specific document from a collection
- Make nested queries in MongoDB 4 to fetch a specific document
- Filter specific values from a MongoDB document
- Fetch specific field values in MongoDB
- Query an array in MongoDB to fetch a specific value
- Project specific array field in a MongoDB collection?
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query to fetch array values
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How to get a specific object from array of objects inside specific MongoDB document?
- Find document with array that contains a specific value in MongoDB
- MongoDB query to remove a specific document
- MongoDB “$and” operator for subcollection to fetch a document?
- How to project specific fields from a document inside an array in Mongodb?

Advertisements