mongo 减去两个字段 [英] mongo subtract two fields

查看:107
本文介绍了mongo 减去两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mongo 集合,其中包含具有两个字段fieldA"和fieldB"的文档,它们是时间戳,我需要获取(fieldB-fieldA)/6000 > 2"的所有文档......所以我正在寻找一些功能来做到这一点......

I have a mongo collection which has documents with two fields "fieldA" and "fieldB", which are timestamps and I need to get all the documents which are "(fieldB-fieldA)/6000 > 2"... So I was looking for some function in order to do this...

我在一些帖子中看到了函数$subtract",但这似乎是在 mongo 2.1.x 中,目前我使用的是 2.0.x(它是稳定版),知道如何用 2.0.x 做到这一点吗?或者我需要创建一个新字段吗?还是只能在应用端做?

I saw in some posts the function "$subtract" but that seems is in mongo 2.1.x and currently I'm using 2.0.x (it's the stable), any idea how to do this with 2.0.x? or Do I need to create a new field? or Can I only do it in the application side?

顺便说一句,我正在使用 ruby​​ mongo 驱动程序...

btw, I'm using the ruby mongo driver...

推荐答案

您可以使用 $where 运算符将查询表达式指定为 Javascript.在外壳中:

You can use the $where operator to specify query expressions as Javascript. In the shell:

db.myCollection.find( { $where: "(this.fieldB - this.fieldA)/6000 > 2" } );

db.myCollection.find( { $where: "(this.fieldB - this.fieldA)/6000 > 2" } );

但是,您应该注意,Javascript 的执行速度可能非常慢.如果这是一次性查询, $where 可能是一个不错的解决方案.否则,将方程的结果存储在单独的字段中是确保查询及时的最佳方式.

You should note, however, that Javascript execution can be very slow. If this is a one time query, $where may be an ok solution. Otherwise, storing the result of the equation in a separate field is the best way to ensure that queries are timely.

有关 $where 运算符的其他信息:http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

Additional information on the $where operator: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

这篇关于mongo 减去两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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