
- 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
Check if value exists for a field in a MongoDB document?
To check if value exists for a field in a MongoDB document, you can use find() along with $exists operator. Let us first create a collection with documents −
> db.checkIfValueDemo.insertOne({"PlayerName":"John Smith","PlayerScores":[5000,98595858,554343]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6f507af8e7a4ca6b2ad98") } > db.checkIfValueDemo.insertOne({"PlayerName":"John Doe","PlayerScores":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6f512af8e7a4ca6b2ad99") } > db.checkIfValueDemo.insertOne({"PlayerName":"Carol Taylor","PlayerScores":[7848474,8746345353]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6f521af8e7a4ca6b2ad9a") } > db.checkIfValueDemo.insertOne({"PlayerName":"David Miller","PlayerScores":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6f531af8e7a4ca6b2ad9b") }
Following is the query to display all documents from a collection with the help of find() method −
> db.checkIfValueDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc6f507af8e7a4ca6b2ad98"), "PlayerName" : "John Smith", "PlayerScores" : [ 5000, 98595858, 554343 ] } { "_id" : ObjectId("5cc6f512af8e7a4ca6b2ad99"), "PlayerName" : "John Doe", "PlayerScores" : [ ] } { "_id" : ObjectId("5cc6f521af8e7a4ca6b2ad9a"), "PlayerName" : "Carol Taylor", "PlayerScores" : [ 7848474, 8746345353 ] } { "_id" : ObjectId("5cc6f531af8e7a4ca6b2ad9b"), "PlayerName" : "David Miller", "PlayerScores" : [ ] }
Following is the query to check if value exists for a field in a document. Here, we are checking for field 'PlayerScores with value [ ] −
> db.checkIfValueDemo.find({'PlayerScores.0' : {$exists: true}}).count();
This will produce the following output
2
- Related Articles
- Return True if a document exists in MongoDB?
- Check that Field Exists with MongoDB?
- Check if MongoDB database exists?
- How can I check whether a field exists or not in MongoDB?
- How to find if element exists in document - MongoDB?
- MongoDB query to exclude if id is equal to a document field array value
- How to apply a condition only if field exists in MongoDB?
- Check if a field contains a string in MongoDB?
- Check for Existing Document in MongoDB?
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Find the document by field name with a specific value in MongoDB?
- Check if a value exists in a column in a MySQL table?
- How to determine whether a field exists in MongoDB?
- Check if value exists in a comma separated list in MySQL?

Advertisements