MongoDb查询条件比较2个字段 [英] MongoDb query condition on comparing 2 fields
本文介绍了MongoDb查询条件比较2个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个集合T,有2个字段:Grade1和Grade2,我想选择条件Grade1> Grade2,如何获得像MySQL中的查询?
选择* from T Where 1> Grade 2
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
推荐答案
只要知道它会相当慢(必须在每个记录上执行Javascript代码),因此结合索引查询,如果可以。
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 } } );
或更紧凑:
db.T.find( { $where : "this.Grade1 > this.Grade2" } );
这篇关于MongoDb查询条件比较2个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文