
- 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 get array of nested string?
To get array of nested string in MongoDB, use dot notation in find(). Let us create a collection with documents −
> db.demo89.insertOne( ... { id: 101, Details: [ { Name: "Chris", Marks: 45 }, { Name: "David", Marks: 55, Subjects : ["MySQL", "MongoDB", "Java", "C"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163b79799acab037af51") } > db.demo89.insertOne( ... { id: 102, Details: [ { Name: "Mike", Marks: 48 }, { Name: "Bob", Marks: 98, Subjects : ["C++", "MySQL"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163c79799acab037af52") }
Display all documents from a collection with the help of find() method −
> db.demo89.find();
This will produce the following output −
{ "_id" : ObjectId("5e2c163b79799acab037af51"), "id" : 101, "Details" : [ { "Name" : "Chris", "Marks" : 45 }, { "Name" : "David", "Marks" : 55, "Subjects" : [ "MySQL", "MongoDB", "Java", "C" ] } ] } { "_id" : ObjectId("5e2c163c79799acab037af52"), "id" : 102, "Details" : [ { "Name" : "Mike", "Marks" : 48 }, { "Name" : "Bob", "Marks" : 98, "Subjects" : [ "C++", "MySQL" ] } ] }
Following is the query to get array of nested string −
> db.demo89.find({ "Details.Subjects": "MongoDB"});
This will produce the following output −
{ "_id" : ObjectId("5e2c163b79799acab037af51"), "id" : 101, "Details" : [ { "Name" : "Chris", "Marks" : 45 }, { "Name" : "David", "Marks" : 55, "Subjects" : [ "MySQL", "MongoDB", "Java", "C" ] } ] }
- Related Articles
- Query array of nested string with MongoDB?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to get only specific fields in nested array documents?
- Write a MongoDB query to get nested value?
- Query a nested field within an array with MongoDB
- MongoDB query to update nested document
- Query nested array by more than one condition in MongoDB
- MongoDB query to get average in aggregation of array element?
- MongoDB query to update the nested document?
- Query MongoDB for a nested search
- Query deeply nested Objects in MongoDB
- MongoDB find() query for nested document?
- MongoDB query to filter object where all elements from nested array match the condition
- Group query upon nested object in MongoDB?

Advertisements