
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 } ] }
Advertisements