
- 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 access an object in an array
To access an object in an array, use dot notation. Let us create a collection with documents −
> db.demo299.insertOne( ... { ... "id":100, ... "Name":"Robert", ... "details":[ ... { ... "SubjectName":["C++","Python"] ... }, ... { ... "SubjectName":["Spring","Hibernate"] ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d685a5d93261e4bc9ea4b") } > > > db.demo299.insertOne( ... { ... "id":101, ... "Name":"Adam", ... "details":[ ... { ... "SubjectName":["Python","JSP"] ... }, ... { ... "SubjectName":["Servlet","Operating System"] ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d685b5d93261e4bc9ea4c") }
Display all documents from a collection with the help of find() method −
> db.demo299.find();
This will produce the following output −
{ "_id" : ObjectId("5e4d685a5d93261e4bc9ea4b"), "id" : 100, "Name" : "Robert", "details" : [ { "SubjectName" : [ "C++", "Python" ] }, { "SubjectName" : [ "Spring", "Hibernate" ] } ] } { "_id" : ObjectId("5e4d685b5d93261e4bc9ea4c"), "id" : 101, "Name" : "Adam", "details" : [ { "SubjectName" : [ "Python", "JSP" ] }, { "SubjectName" : [ "Servlet", "Operating System" ] } ] }
Following is the query to access an object in an array −
> db.demo299.find({"details.SubjectName":"Servlet"});
This will produce the following output −
{ "_id" : ObjectId("5e4d685b5d93261e4bc9ea4c"), "id" : 101, "Name" : "Adam", "details" : [ { "SubjectName" : [ "Python", "JSP" ] }, { "SubjectName" : [ "Servlet", "Operating System" ] } ] }
- Related Articles
- MongoDB query for Partial Object in an array
- MongoDB query to find data from an array inside an object?
- MongoDB query to remove empty objects in an object-array?
- Query on the last object of an array with MongoDB
- MongoDB query to replace value in an array?
- MongoDB query to push document into an array
- MongoDB query to update an array using FindAndUpdate()?
- MongoDB query to change simple field into an object?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB query to set a sub item in an array?
- Query an array in MongoDB to fetch a specific value
- MongoDB query to return specific fields from an array?
- MongoDB query to update array object in index N?
- MongoDB query for capped sub-collection in an array
- How to insert an item to an array that is inside an object in MongoDB?

Advertisements