
- 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
Perform nested document value search in MongoDB?
For searching value, use $match in MongoDB. Let us create a collection with documents −
> db.demo648.insertOne( ... { ... StudentInformation: ... [ ... { ... Name:"John", ... CountryName:"US" ... }, ... { ... Name:"David", ... CountryName:"AUS" ... }, ... { ... Name:"Chris", ... CountryName:"US" ... }, ... { ... Name:"Robert", ... CountryName:"UK" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9c8b286c954c74be91e6f5") }
Display all documents from a collection with the help of find() method −
> db.demo648.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c8b286c954c74be91e6f5"), "StudentInformation" : [ { "Name" : "John", "CountryName" : "US" }, { "Name" : "David", "CountryName" : "AUS" }, { "Name" : "Chris", "CountryName" : "US" }, { "Name" : "Robert", "CountryName" : "UK" } ] }
Following is the query to search for value in MongoDB −
> db.demo648.aggregate([ ... { $unwind: "$StudentInformation" }, ... { $match: { "StudentInformation.CountryName": "US" } }, ... { $project: {_id: 0}} ... ])
This will produce the following output −
{ "StudentInformation" : { "Name" : "John", "CountryName" : "US" } } { "StudentInformation" : { "Name" : "Chris", "CountryName" : "US" } }
- Related Articles
- Updating nested document in MongoDB
- Set condition in MongoDB nested document?
- MongoDB find() query for nested document?
- MongoDB query to update nested document
- Perform MongoDB full text search
- Query MongoDB for a nested search
- MongoDB query to update the nested document?
- In MongoDB how do you use $set to update a nested value/embedded document?
- How to search document in MongoDB by _id
- Using find() to search for nested keys in MongoDB?
- Make nested queries in MongoDB 4 to fetch a specific document
- Search a string with special characters in a MongoDB document?
- MongoDB Increment value inside nested array?
- How to perform $gt on a hash in a MongoDB document?
- How can I search a collection to find a nested value in one of its documents in MongoDB?

Advertisements