- 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
Querying with MongoDB subelement?
You can use positional operator $ for this. Let us first create a collection with documents −
> db.subElementQueryingDemo.insertOne( ... { ... "ClientName":"Chris", ... "Status": [ { "isMarried": true }, { "isMarried": false } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccf28c9dceb9a92e6aa1953") }
Following is the query to display all documents from a collection with the help of find() method −
> db.subElementQueryingDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf28c9dceb9a92e6aa1953"), "ClientName" : "Chris", "Status" : [ { "isMarried" : true }, { "isMarried" : false } ] }
Here is how you can query sublement in MongoDB −
> db.subElementQueryingDemo.find({ "Status.isMarried": true }, {ClientName: 1, 'Status.$': 1}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf28c9dceb9a92e6aa1953"), "ClientName" : "Chris", "Status" : [ { "isMarried" : true } ] }
- Related Articles
- Querying array elements with MongoDB?
- Querying null value in MongoDB?
- Get at least one match in list querying with MongoDB?
- Querying from part of object in an array with MongoDB
- Querying internal array size in MongoDB?
- Querying an array of arrays in MongoDB?
- How to improve querying field in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- Fetch records in MongoDB on querying its subset
- Querying object's field array values in MongoDB?
- Native Querying MongoDB inside array and get the count
- Querying array of Embedded Documents in MongoDB based on Range?
- How to use arrays as filters by querying subdocuments in MongoDB?
- Querying only the field name and display only the id in MongoDB?
- Can I utilize indexes when querying by MongoDB subdocument without known field names?

Advertisements