Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the geolocation index in MongoDB

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the geographical location index in MongoDB. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Background

When we open some App, we often have a function of "query the surrounding scenic spots". For example, I found the nearby scenic spots in Gaud.

If this kind of calculation is done through an ordinary relational database, the performance requirements of the server and database are too high.

We can do this through the address location index that comes with MongoDB.

How to use

Preparation: inserting data

Let's insert four pieces of data first, that is, the type, name and geographical location of the store. Note that the first value of the geographic location represents precision (range-100 to 100), and the second value represents latitude (range-90 to 90).

Query result

The statement in the following figure is to query stores near latitude and longitude 50 and 50, but we see that there is an error because we want to create a new geographic index above the field loc.

The steps to build a geographic index are as follows: (that is, to build a geographic index on top of the loc field)

We repeated the previous statement and found that it was successful.

Now let's write a slightly more complex scenario, where we will search for coffee shops near the location (50, 50), which is sorted from near to far.

Let's write a more complex scene, we want to search the coffee shop near the location (50jue 50), and the distance is within 37m.

Another parameter is limit, which controls the number of stores, as follows:

This is what the geographic location index in MongoDB is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report