比较 2 个字段的 MongoDb 查询条件 [英] MongoDb query condition on comparing 2 fields

查看:32
本文介绍了比较 2 个字段的 MongoDb 查询条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合 T,有 2 个字段:Grade1Grade2,我想选择条件为 Grade1 的那些>Grade2,我怎样才能像在 MySQL 中一样得到一个查询?

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

推荐答案

您可以使用 $where.请注意它会相当慢(必须对每条记录执行 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 v.3.6+ 更新

您可以使用 $expr最近的答案

这篇关于比较 2 个字段的 MongoDb 查询条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆