
- 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
Query array of nested string with MongoDB?
To query array of nested string, you can use the dot(.) notation. Let us first create a collection with documents −
> db.nestedStringDemo.insertOne( { "CustomerName": "John", "CustomerOtherDetails": [ { "Age":29, "CountryName": "US" }, { "CompanyName": "Amazon", "Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea4629ef71edecf6a1f690") } > db.nestedStringDemo.insertOne( { "CustomerName": "Chris", "CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" }, { "CompanyName": "Google", "Salary": 250000, "ProjectName": ["Chat Application", "Game Design"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea466eef71edecf6a1f691") }
Following is the query to display all documents from a collection with the help of find() method −
> db.nestedStringDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cea4629ef71edecf6a1f690"), "CustomerName" : "John", "CustomerOtherDetails" : [ { "Age" : 29, "CountryName" : "US" }, { "CompanyName" : "Amazon", "Salary" : 150000, "ProjectName" : [ "Online Library Management System", "Pig Dice Game" ] } ] } { "_id" : ObjectId("5cea466eef71edecf6a1f691"), "CustomerName" : "Chris", "CustomerOtherDetails" : [ { "Age" : 27, "CountryName" : "AUS" }, { "CompanyName" : "Google", "Salary" : 250000, "ProjectName" : [ "Chat Application", "Game Design" ] } ] }
Now, let us query an array of nested string using dot notation −
> db.nestedStringDemo.find({"CustomerOtherDetails.ProjectName":"Chat Application"}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cea466eef71edecf6a1f691"), "CustomerName" : "Chris", "CustomerOtherDetails" : [ { "Age" : 27, "CountryName" : "AUS" }, { "CompanyName" : "Google", "Salary" : 250000, "ProjectName" : [ "Chat Application", "Game Design" ] } ] }
- Related Articles
- MongoDB query to get array of nested string?
- Query a nested field within an array with MongoDB
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- Query nested array by more than one condition in MongoDB
- MongoDB query to get only specific fields in nested array documents?
- Create array with MongoDB query?
- Query MongoDB for a nested search
- Query deeply nested Objects in MongoDB
- MongoDB find() query for nested document?
- MongoDB query to update nested document
- MongoDB query with $all in array
- Group query upon nested object in MongoDB?
- MongoDB query to update the nested document?
- MongoDB $push in nested array?

Advertisements