Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo field A greater than field B

Tags:

mongodb

i'm trying a simple query in Mongo which in MySQL would look like this.

select * from emails where bounceCount > sentCount; 

So far I have.

db.email.find({ bounceCount : { $gt : sentCount } } ); 

But I get this error

JS Error: ReferenceError: sentCount is not defined (shell):0 

How do I reference the sentCount in that shell?

like image 640
Mitesh Avatar asked Apr 16 '13 16:04

Mitesh


People also ask

How do you do greater than in MongoDB?

You can use the following operators in MongoDB to perform greater than or less than queries: $lt: Less than. $lte: Less than or equal. $gt: Greater than.

How do I match two fields in MongoDB?

MongoDB compare two fields in the same document You can use the $where operator to compare the fields.

What does $GT mean MongoDB?

Overview. $gt means “greater than.” It is a query operator and one of the comparison operators in MongoDB. It selects or matches documents with a path or field value greater than the specified value.

Is $gt for greater than or equal to the value?

Definition. $gt selects those documents where the value of the field is greater than (i.e. > ) the specified value . For most data types, comparison operators only perform comparisons on fields where the BSON type matches the query value's type. MongoDB supports limited cross-BSON comparison through Type Bracketing.


Video Answer


1 Answers

It should do the trick:

db.emails.find({ $expr: { $gt: [ "$bounceCount" , "$sentCount" ] } }); 

Here is the reference where I found it: https://docs.mongodb.com/manual/reference/operator/query/expr/#op._S_expr

like image 182
Andrei Petrik Avatar answered Sep 20 '22 13:09

Andrei Petrik