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?
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.
MongoDB compare two fields in the same document You can use the $where operator to compare the fields.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With