- 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 documents that contains specific field in MongoDB?
For this, use the $exists operator. Let us first create a collection with documents −
>dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product1":10,"Pr oduct2":50}}); { "acknowledged" : true, "insertedId" : ObjectId("5cf2385bb64a577be5a2bc14") } >dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product3":150,"P roduct7":100,"Product5":250}}); { "acknowledged" : true, "insertedId" : ObjectId("5cf2387eb64a577be5a2bc15") }
Following is the query to display all documents from a collection with the help of find() method −
> dbfindDocumentContainsSpecificFieldDemofind()pretty();
This will produce the following document −
{ "_id" : ObjectId("5cf2385bb64a577be5a2bc14"), "ProductPrices" : { "Product1" : 10, "Product2" : 50 } } { "_id" : ObjectId("5cf2387eb64a577be5a2bc15"), "ProductPrices" : { "Product3" : 150, "Product7" : 100, "Product5" : 250 } }
Following is the query to find documents that contains specific field −
> dbfindDocumentContainsSpecificFieldDemofind({"ProductPricesProduct2":{$exists:true}});
This will produce the following document −
{ "_id" : ObjectId("5cf2385bb64a577be5a2bc14"), "ProductPrices" : { "Product1" : 10, "Product2" : 50 } }
- Related Articles
- Find MongoDB documents that contains specific field?
- 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?
- Find value above a specific value in MongoDB documents?
- 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?
- Fetch specific multiple documents in MongoDB
- Sum MongoDB Sub-documents field?
- MongoDB query to match documents that contain an array field
- Find all collections in MongoDB with specific field?
- How to find MongoDB documents with a specific string?
- Find a strict document that contains only a specific field with a fixed length?

Advertisements