
- 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
MongoDB query condition on comparing 2 fields?
To query condition on comparing 2 fields, use the following syntax −
db.yourCollectionName.find( { $where: function() { return this.yourFirstFieldName < this.yourSecondFieldName } } ).pretty();
To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.comparingTwoFieldsDemo.insertOne({"StudentName":"John","StudentAge":21,"StudentMathMarks":99,"StudentPhysicsMarks":98}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac09e6cea1f28b7aa0807") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Carol","StudentAge":22,"StudentMathMarks":79,"StudentPhysicsMarks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0b46cea1f28b7aa0808") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"David","StudentAge":24,"StudentMathMarks":39,"StudentPhysicsMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0c96cea1f28b7aa0809") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":87,"StudentPhysicsMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac0e06cea1f28b7aa080a") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.comparingTwoFieldsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ac09e6cea1f28b7aa0807"), "StudentName" : "John", "StudentAge" : 21, "StudentMathMarks" : 99, "StudentPhysicsMarks" : 98 } { "_id" : ObjectId("5c8ac0b46cea1f28b7aa0808"), "StudentName" : "Carol", "StudentAge" : 22, "StudentMathMarks" : 79, "StudentPhysicsMarks" : 89 } { "_id" : ObjectId("5c8ac0c96cea1f28b7aa0809"), "StudentName" : "David", "StudentAge" : 24, "StudentMathMarks" : 39, "StudentPhysicsMarks" : 45 } { "_id" : ObjectId("5c8ac0e06cea1f28b7aa080a"), "StudentName" : "Bob", "StudentAge" : 23, "StudentMathMarks" : 87, "StudentPhysicsMarks" : 78 }
Here is the query to condition on comparing 2 fields −
> db.comparingTwoFieldsDemo.find( { $where: function() { return this.StudentMathMarks < this.StudentPhysicsMarks } } ).pretty();
The following is the output −
{ "_id" : ObjectId("5c8ac0b46cea1f28b7aa0808"), "StudentName" : "Carol", "StudentAge" : 22, "StudentMathMarks" : 79, "StudentPhysicsMarks" : 89 } { "_id" : ObjectId("5c8ac0c96cea1f28b7aa0809"), "StudentName" : "David", "StudentAge" : 24, "StudentMathMarks" : 39, "StudentPhysicsMarks" : 45 }
- Related Articles
- MongoDB query condition to compare two fields?
- How to query a document in MongoDB comparing fields from an array?
- MySQL query to update different fields based on a condition?
- MongoDB query for exact match on multiple document fields
- MongoDB query to update selected fields
- MongoDB query to sum specific fields
- How to query documents by a condition on the subdocument in MongoDB?
- MongoDB query to update only certain fields?
- MongoDB query for fields in embedded document?
- MongoDB query with fields in the same document?
- MongoDB query to group several fields using aggregation framework?
- Sort array in MongoDB query and project all fields?
- MongoDB query to exclude both the fields with FALSE
- MongoDB query to return specific fields from an array?
- MongoDB query to check the existence of multiple fields

Advertisements