
- 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
How to apply a condition only if field exists in MongoDB?
You can use $or operator for this. Let us first create a collection with documents −
> db.applyConditionDemo.insertOne({"StudentName":"Larry","StudentAge":21,"StudentMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80b78623186894665ae36") } > db.applyConditionDemo.insertOne({"StudentName":"Sam","StudentAge":23,"StudentMarks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80b87623186894665ae37") } > db.applyConditionDemo.insertOne({"StudentName":"David","StudentAge":21,"StudentMarks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80b95623186894665ae38") } > db.applyConditionDemo.insertOne({"StudentName":"Carol","StudentAge":24,"StudentMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80ba3623186894665ae39") } > db.applyConditionDemo.insertOne({"StudentName":"Chris","StudentAge":21,"StudentMarks":88}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80bae623186894665ae3a") } > db.applyConditionDemo.insertOne({"StudentName":"Robert","StudentMarks":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cb80c3d623186894665ae3b") }
Following is the query to display all documents from a collection with the help of find() method −
> db.applyConditionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb80b78623186894665ae36"), "StudentName" : "Larry", "StudentAge" : 21, "StudentMarks" : 45 } { "_id" : ObjectId("5cb80b87623186894665ae37"), "StudentName" : "Sam", "StudentAge" : 23, "StudentMarks" : 55 } { "_id" : ObjectId("5cb80b95623186894665ae38"), "StudentName" : "David", "StudentAge" : 21, "StudentMarks" : 65 } { "_id" : ObjectId("5cb80ba3623186894665ae39"), "StudentName" : "Carol", "StudentAge" : 24, "StudentMarks" : 78 } { "_id" : ObjectId("5cb80bae623186894665ae3a"), "StudentName" : "Chris", "StudentAge" : 21, "StudentMarks" : 88 } { "_id" : ObjectId("5cb80c3d623186894665ae3b"), "StudentName" : "Robert", "StudentMarks" : 98 }
Following is the query to apply a condition only if field exists −
> db.applyConditionDemo.find({ $or: [ { StudentAge: { $exists:false } }, { StudentAge:21 } ]}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb80b78623186894665ae36"), "StudentName" : "Larry", "StudentAge" : 21, "StudentMarks" : 45 } { "_id" : ObjectId("5cb80b95623186894665ae38"), "StudentName" : "David", "StudentAge" : 21, "StudentMarks" : 65 } { "_id" : ObjectId("5cb80bae623186894665ae3a"), "StudentName" : "Chris", "StudentAge" : 21, "StudentMarks" : 88 } { "_id" : ObjectId("5cb80c3d623186894665ae3b"), "StudentName" : "Robert", "StudentMarks" : 98 }
- Related Articles
- How to determine whether a field exists in MongoDB?
- Check if value exists for a field in a MongoDB document?
- Apply a condition inside subset in MongoDB Aggregation?
- How to return only value of a field in MongoDB?
- Check that Field Exists with MongoDB?
- How can I check whether a field exists or not in MongoDB?
- How to find if element exists in document - MongoDB?
- How to run MongoDB query to update only a specific field value?
- Selecting only a single field from MongoDB?
- How to check if field is a number in MongoDB?
- How to check if a field in MongoDB is [] or {}?
- Return True if a document exists in MongoDB?
- Check if MongoDB database exists?
- How to select a field corresponding to the field in which MAX() exists?
- How to select objects where an array contains only a specific field in MongoDB?

Advertisements