perform math in searchqueries mongodb

流过昼夜 提交于 2019-12-24 13:52:14

问题


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;

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


回答1:


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

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).

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.



来源:https://stackoverflow.com/questions/23282193/perform-math-in-searchqueries-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!