在搜索查询中执行数学mongodb [英] perform math in searchqueries mongodb

查看:118
本文介绍了在搜索查询中执行数学mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在find函数中执行数学运算 MySQL的?我有以下sql查询,用于根据以下内容执行搜索 MySQL中的半径:

Is there a way to perform math in the find function as is possible in MySQL? I have the following sql query for performing searches based on radius in MySQL:

SELECT id,(3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos( radians(lng) -     radians(-122)) + sin(radians(37)) * sin(radians(lat)))) 
AS distance 
FROM markers 
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20;

并且希望能够在mongodb或node.js中执行类似的操作. 这可能吗?

And would like to be able to perform something similar in mongodb or node.js. Is this possible?

推荐答案

MongoDB查询语言和聚合框架仅具有非常有限的数学运算符.

The MongoDB query language and aggregation framework only has very limited mathematical operators.

但是MongoDB可以使用 $ where -运算符.这使您可以创建条件非常复杂的查找查询.但是请记住,这种方法相当慢(尽管不一定比SQL慢得多).

But MongoDB can use arbitrary Javascript functions to filter results with the $where - operator. This allows you to create find-queries with very complex conditions. But keep in mind that this method is quite slow (although not necessarily much slower than in SQL).

但是查看您的列名,您似乎正在使用地理数据. MongoDB具有功能丰富且高效的框架,可用于索引和查询地理空间数据 .与直接处理三角函数相比,使用此选项可能也是一种选择,它将比使用三角函数更快,更容易使用.

But looking at your column names, you seem to be working with geographical data. MongoDB has a very feature-rich and efficient framework for indexing and querying geospatial data. Using this might also be an option for you which would be a lot faster and much easier to use than dealing with trigonometric functions directly.

这篇关于在搜索查询中执行数学mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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