
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Find the document by field name with a specific value in MongoDB?
To find the document by field name with a specific value, you can use $exists operator. Let us create a collection with documents
> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9e93b2d628fa4220163b64") } > db.findByFieldName.insertOne({ ... " Client":{ ... " ClientDetails":{ ... " ClientName":"Chris", ... " ClientAge":27 ... }, ... "ClientEducationDetails":{ ... " isEducated":true, ... "CollegeName":"M.I.T." ... ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9e9421d628fa4220163b65") }
Following is the query to display all documents from a collection with the help of find() method
> db.findByFieldName.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9e93b2d628fa4220163b64"), "Client" : { "ClientDetails" : { "ClientName" : "Larry", "ClientAge" : 29 }, "ClientProjectDetails" : { "ProjectName" : "Online Book Store", "TeamSize" : 10, "TechnologyUsed" : "Spring Boot" } } } { "_id" : ObjectId("5c9e9421d628fa4220163b65"), "Client" : { "ClientDetails" : { "ClientName" : "Chris", "ClientAge" : 27 }, "ClientEducationDetails" : { "isEducated" : true, "CollegeName" : "M.I.T." } } }
Following is the query to find the document by field name
> db.findByFieldName.find({"Client.ClientProjectDetails":{$exists: true}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9e93b2d628fa4220163b64"), "Client" : { "ClientDetails" : { "ClientName" : "Larry", "ClientAge" : 29 }, "ClientProjectDetails" : { "ProjectName" : "Online Book Store", "TeamSize" : 10, "TechnologyUsed" : "Spring Boot" } } }
- Related Articles
- Find document with array that contains a specific value in MongoDB
- Find MongoDB document with array containing the maximum occurrence of a specific value
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Update only a specific value in a MongoDB document
- Find all collections in MongoDB with specific field?
- How to find a document by the non-existence of a field in MongoDB?
- Search by specific field in MongoDB
- MongoDB query select and display only a specific field from the document?
- MongoDB aggregation to fetch documents with specific field value?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Extract a MongoDB document with a specific string
- Check if value exists for a field in a MongoDB document?
- MongoDB query to pull a specific value from a document
- Fetch a specific document in MongoDB with array elements

Advertisements