- 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
How to fire find query on sub-documents in MongoDB?
For sub-documents, use the dot notation. Let us first create a collection with documents −
> db.demo537.insertOne({"details":{"SubjectName":"MongoDB"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a10ef4dcbee04fbbc05") } > db.demo537.insertOne({"details":{"SubjectName":"MySQL"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a4bef4dcbee04fbbc06") } > db.demo537.insertOne({"details":{"SubjectName":"Java"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a51ef4dcbee04fbbc07") }
Display all documents from a collection with the help of find() method −
> db.demo537.find();
This will produce the following output −
{ "_id" : ObjectId("5e8c8a10ef4dcbee04fbbc05"), "details" : { "SubjectName" : "MongoDB" } } { "_id" : ObjectId("5e8c8a4bef4dcbee04fbbc06"), "details" : { "SubjectName" : "MySQL" } } { "_id" : ObjectId("5e8c8a51ef4dcbee04fbbc07"), "details" : { "SubjectName" : "Java" } }
Following is the query to fire query on sub-documents in MongoDB −
> db.demo537.count({'details.SubjectName': 'MongoDB'})
This will produce the following output −
1
- Related Articles
- Filter sub documents by sub document in MongoDB?
- Sum MongoDB Sub-documents field?
- MongoDB filter multiple sub-documents?
- How to query documents by a condition on the subdocument in MongoDB?
- How would I filter out sub documents in MongoDB?
- MongoDB query to skip documents
- MongoDB - Query embedded documents?
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query by sub-field?
- How to update many documents with one query in MongoDB?
- Finding highest value from sub-arrays in MongoDB documents?
- MongoDB query to skip n first documents?
- MongoDB query to find matching documents given an array with values?

Advertisements