MongoDb query condition on comparing 2 fields

前端 未结 4 907
面向向阳花
面向向阳花 2020-11-22 00:44

I have a collection T, with 2 fields: Grade1 and Grade2, and I want to select those with condition Grade1 > Grade2, ho

4条回答
  •  耶瑟儿~
    2020-11-22 01:35

    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

提交回复
热议问题