- 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
Find MongoDB documents that contains specific field?
To find documents that contain specific field, use $exists. Let us create a collection with documents −
> db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"Chris","ClientAge":34}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2a31627c0c63e7dba69") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"John","ClientLastName":"Smith","ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2be1627c0c63e7dba6a") } > db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David","ClientAge":33}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2cd1627c0c63e7dba6b") } >db.demo247.insertOne({"ClientDetails":[{"ClientFirstName":"David","ClientLastName":"Miller","ClientAge":31}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b2de1627c0c63e7dba6c") }
Display all documents from a collection with the help of find() method −
> db.demo247.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e46b2a31627c0c63e7dba69"), "ClientDetails" : [ { "ClientFirstName" : "Chris", "ClientAge" : 34 } ] } { "_id" : ObjectId("5e46b2be1627c0c63e7dba6a"), "ClientDetails" : [ { "ClientFirstName" : "John", "ClientLastName" : "Smith", "ClientAge" : 31 } ] } { "_id" : ObjectId("5e46b2cd1627c0c63e7dba6b"), "ClientDetails" : [ { "ClientFirstName" : "David", "ClientAge" : 33 } ] } { "_id" : ObjectId("5e46b2de1627c0c63e7dba6c"), "ClientDetails" : [ { "ClientFirstName" : "David", "ClientLastName" : "Miller", "ClientAge" : 31 } ] }
Following is the query to find documents that contains specific field −
> db.demo247.find({"ClientDetails.ClientLastName":{$exists:true}});
This will produce the following output −
{ "_id" : ObjectId("5e46b2be1627c0c63e7dba6a"), "ClientDetails" : [ { "ClientFirstName" : "John", "ClientLastName" : "Smith", "ClientAge" : 31 } ] } { "_id" : ObjectId("5e46b2de1627c0c63e7dba6c"), "ClientDetails" : [ { "ClientFirstName" : "David", "ClientLastName" : "Miller", "ClientAge" : 31 } ] }
- Related Articles
- Find documents that contains specific field in MongoDB?
- Get MongoDB documents that contains specific attributes in array
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- Find document with array that contains a specific value in MongoDB
- Get count of array elements from a specific field in MongoDB documents?
- Sum MongoDB Sub-documents field?
- MongoDB query to match documents that contain an array field
- MongoDB query to add up the values of a specific field in documents
- How do I find all documents with a field that is NaN in MongoDB?
- How to find MongoDB documents with a specific string?
- Find value above a specific value in MongoDB documents?
- Find a strict document that contains only a specific field with a fixed length?
- Find which MongoDB document contains a specific string?
- Match MongoDB documents with field value greater than a specific number and fetch them?

Advertisements