
- 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
Get array items inside a MongoDB document?
To get array items in a MongoDB document, use the dot(.) notation. Let us create a collection with documents −
> db.demo29.insertOne({"StudentDetails":[{"StudentName":"Chris","StudentMarks":58},{"StudentName":"Bob","StudentMarks":69}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fcc08f2315c2efc48e6e") } >db.demo29.insertOne({"StudentDetails":[{"StudentName":"David","StudentMarks":97},{"StudentName":"Carol","StudentMarks":96}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fcd38f2315c2efc48e6f") }
Display all documents from a collection with the help of find() method −
> db.demo29.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e15fcc08f2315c2efc48e6e"), "StudentDetails" : [ { "StudentName" : "Chris", "StudentMarks" : 58 }, { "StudentName" : "Bob", "StudentMarks" : 69 } ] } { "_id" : ObjectId("5e15fcd38f2315c2efc48e6f"), "StudentDetails" : [ { "StudentName" : "David", "StudentMarks" : 97 }, { "StudentName" : "Carol", "StudentMarks" : 96 } ] }
Following is the query to get array items inside a MongoDB document −
> db.demo29.find({"StudentDetails.StudentName":"David"});
This will produce the following output −
{ "_id" : ObjectId("5e15fcd38f2315c2efc48e6f"), "StudentDetails" : [ { "StudentName" : "David", "StudentMarks" : 97 }, { "StudentName" : "Carol", "StudentMarks" : 96 } ] }
- Related Articles
- How to get the matching document inside an array in MongoDB?
- How to get a specific object from array of objects inside specific MongoDB document?
- MongoDB syntax for updating an object inside an array within a document?
- How to project specific fields from a document inside an array in Mongodb?
- Native Querying MongoDB inside array and get the count
- How to get items from an object array in MongoDB?
- MongoDB Aggregate to get average from document and of array elements?
- How to push new items to an array inside of an object in MongoDB?
- Clearing items in a nested MongoDB array?
- Increment MongoDB value inside a nested array
- How to add a sub-document to sub-document array in MongoDB?
- MongoDB Aggregation to slice array inside array
- How to get embedded data in a MongoDB document?
- Get the top most document from a MongoDB collection
- MongoDB query to get last inserted document?

Advertisements