- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding a specific item in subdocument with MongoDB?
To get a specific item in a sudocument, use the dot(.) notation. Let us create a collection with documents −
> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol","StudentSubject":"Java"},{ "StudentName" : "David", "StudentSubject" : "MongoDB" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike","StudentSubject":"Python"},{ "StudentName" : "David", "StudentSubject" : "C" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf70471bf0181ecc4229e") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Jace","StudentSubject":"C++"},{ "StudentName" : "John", "StudentSubject" : "MySQL" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf72071bf0181ecc4229f") }
Display all documents from a collection with the help of find() method −
> db.demo81.find();
This will produce the following output −
{ "_id" : ObjectId("5e2bf6ec71bf0181ecc4229d"), "StudentDetails" : [ { "StudentName" : "Carol", "StudentSubject" : "Java" }, {"StudentName" : "David", "StudentSubject" : "MongoDB" } ] } { "_id" : ObjectId("5e2bf70471bf0181ecc4229e"), "StudentDetails" : [ { "StudentName" : "Mike", "StudentSubject" : "Python" }, { "StudentName" : "David", "StudentSubject" : "C" } ] } { "_id" : ObjectId("5e2bf72071bf0181ecc4229f"), "StudentDetails" : [ { "StudentName" : "Jace", "StudentSubject" : "C++" }, { "StudentName" : "John", "StudentSubject" : "MySQL" } ] }
Following is the query to find item in a subdocument with MongoDB −
> db.demo81.find({"StudentDetails.StudentSubject":"MongoDB"});
This will produce the following output −
{ "_id" : ObjectId("5e2bf6ec71bf0181ecc4229d"), "StudentDetails" : [ { "StudentName" : "Carol", "StudentSubject" : "Java" }, { "StudentName" : "David", "StudentSubject" : "MongoDB" } ] }
- Related Articles
- How to select a specific subdocument in MongoDB?
- How to filter array in subdocument with MongoDB?
- Sort by subdocument in MongoDB
- MongoDB query to find and return subdocument with criteria?
- Display MongoDB with document and subdocument example and update
- MongoDB to sort by subdocument match?
- Finding word starting with specific letter in JavaScript
- Finding matching records with LIKE in MongoDB?
- MongoDB query to remove subdocument from document?
- Finding two prime numbers with a specific number gap in JavaScript
- How to sort, select and query subdocument in MongoDB?
- Extract a MongoDB document with a specific string
- Fetch a specific document in MongoDB with array elements
- Push new key element into subdocument of MongoDB?
- Query MongoDB subdocument to print on one line?

Advertisements