
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Multiple Properties in MongoDB
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 ] }
Advertisements