mongodb find by comparing field values

前端 未结 3 656

Is it possible to express the following SQL query in mongodb:

SELECT * FROM table AS t WHERE t.field1 > t.filed2;

edit:

3条回答
  •  没有蜡笔的小新
    2020-12-14 12:59

    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" } );
    

提交回复
热议问题