Question
I have a collection T
, with 2 fields: Grade1
and Grade2
, and I want to
select those with condition Grade1 > Grade2
, how can I get a query like in
MySQL?
Select * from T Where Grade1 > Grade2
Answer
You can use a $where. Just be aware it will be fairly slow (has to execute Javascript code on every record) so combine with indexed queries if you can.
db.T.find( { $where: function() { return this.Grade1 > this.Grade2 } } );
or more compact:
db.T.find( { $where : "this.Grade1 > this.Grade2" } );
UPD for mongodb v.3.6+
you can use $expr
as described in recent
answer