
- 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 find and return subdocument with criteria?
Let us create a collection with documents −
> db.demo345.insertOne({ ... "UserName" : "Robert", ... "UserDetails" : [ ... { ... "isMarried" : false, ... "CountryName":"US" ... ... }, ... { ... "isMarried" : true, ... "CountryName":"UK" ... ... }, ... { ... "isMarried" : false, ... "CountryName":"AUS" ... ... } ... ] ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e5404fdf8647eb59e5620ac") }
Display all documents from a collection with the help of find() method −
> db.demo345.find();
This will produce the following output −
{ "_id" : ObjectId("5e5404fdf8647eb59e5620ac"), "UserName" : "Robert", "UserDetails" : [ { "isMarried" : false, "CountryName" : "US" }, { "isMarried" : true, "CountryName" : "UK" }, { "isMarried" : false, "CountryName" : "AUS" } ] }
Following is the query to find and then return subdocument with criteria −
> db.demo345.aggregate([ ... { ... $match: { UserName: "Robert" } ... }, ... { ... $addFields: { ... UserDetails: { ... $filter: { ... input: "$UserDetails", ... cond: { ... $eq: [ "$$this.isMarried", true ] ... } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : ObjectId("5e5404fdf8647eb59e5620ac"), "UserName" : "Robert", "UserDetails" : [ { "isMarried" : true, "CountryName" : "UK" } ] }
- Related Articles
- Query MongoDB with length criteria?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB query to remove subdocument from document?
- How to sort, select and query subdocument in MongoDB?
- Query MongoDB subdocument to print on one line?
- Display MongoDB with document and subdocument example and update
- How to filter array in subdocument with MongoDB?
- MongoDB query on nth element (variable index) of subdocument array
- How to query documents by a condition on the subdocument in MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Find value in a MongoDB Array with multiple criteria?
- MongoDB query to count records on the basis of matching criteria
- MongoDB query to return only embedded document?
- Finding a specific item in subdocument with MongoDB?
- MongoDB to sort by subdocument match?

Advertisements