Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inside mechanism of geospatial indexing in mongodb

Anyone knows how the geospatial indexing works, i mean the algorithm to calculate nearest points?

In SQL we may do things like this:
SELECT id, (x-a)*(x-a)+(y-b)*(y-b) as distance FROM table1 ORDER by distance ASC
Sure this is not efficient enough compared with mongodb's geospatial indexing, but how does mongodb calculate and sort?

Many thanks in advance.

like image 515
adamsmith Avatar asked Dec 27 '11 04:12

adamsmith


People also ask

What is geospatial index in MongoDB?

MongoDB's geospatial indexing allows you to efficiently execute spatial queries on a collection that contains geospatial shapes and points.

Does MongoDB support geospatial index?

MongoDB provides the following geospatial index types to support the geospatial queries.

What is geospatial indexing?

With the spatio-temporal library, you can use functions to index points within a region, on a region containing points, and points within a radius to enable fast queries on this data during location analysis.

What is 2dsphere index in MongoDB?

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.


2 Answers

Heart of mongodb geospatial is Geohashes. Geohash is a

Hierarchical spatial data structure which subdivides space into buckets of grid shape.

I couldn't find the appropriate links for the geohash implementations in mongo, but this thread might give some insights.

like image 70
RameshVel Avatar answered Sep 28 '22 05:09

RameshVel


from the 10gen site:

The current implementation encodes geographic hash codes atop standard MongoDB B-trees. Results of $near queries are exact. One limitation with this encoding, while fast, is that prefix lookups don't give exact results, especially around bit flip areas. MongoDB solves this by doing a grid-neighbor search after the initial prefix scan to pick up any straggler points. This generally ensures that performance remains very high while providing correct results.

like image 27
Tyler Brock Avatar answered Sep 28 '22 05:09

Tyler Brock