
- 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 compare multiple properties in MongoDB?n
To compare multiple properties, use $where in MongoDB. Let us create a collection with documents −
> db.demo223.insertOne({"Scores":[56,78]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4ca03d395bdc2134730") } > db.demo223.insertOne({"Scores":[88,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d103d395bdc2134731") } > db.demo223.insertOne({"Scores":[98,79]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d803d395bdc2134732") }
Display all documents from a collection with the help of find() method −
> db.demo223.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ee4ca03d395bdc2134730"), "Scores" : [ 56, 78 ] } { "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : [ 98, 79 ] }
Following is the query to compare multiple properties in MongoDB −
> db.demo223.find({ $where : "this.Scores[0] > this.Scores[1]" });
This will produce the following output −
{ "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : [ 98, 79 ] }
- Related Articles
- Compare multiple properties in MongoDB?
- How to compare field values in MongoDB?
- How to delete multiple ids in MongoDB?
- How to merge multiple documents in MongoDB?
- How to update multiple documents in MongoDB?
- How to apply multiple CSS properties using jQuery?
- How to compare if multiple cells are equal in Excel?
- How to compare two fields in aggregation filter with MongoDB?
- How to update array with multiple conditions in MongoDB
- How to delete multiple documents in MongoDB using deleteMany()?
- How to compare attributes of different objects in MongoDB object array?
- How to ignore the multiple properties of a JSON object in Java?
- How to efficiently perform “distinct” with multiple keys in MongoDB?
- MongoDB query condition to compare two fields?
- Search multiple fields for multiple values in MongoDB?

Advertisements