MongoDB geospatial queries can interpret geometry on a flat surface or a sphere.
2dsphere indexes support only spherical queries (i.e. queries that interpret geometries on a spherical surface).
2d indexes support flat queries (i.e. queries that interpret geometries on a flat surface) and some spherical queries. While 2d indexes support some spherical queries, the use of 2d indexes for these spherical queries can result in error. If possible, use 2dsphere indexes for spherical queries.
With above, I’m still a bit unclear whether to use the 2dsphere or the 2d index, even almost everywhere I read, I saw recommendations to use 2dsphere indexes whenever they talk about the 2d index.
So my intuitive sense is that
the 2dsphere indexes would be a little bit expensive than the 2d index, because there are more calculation involved.
the spherical queries is only necessary when the distance is rather long, when the spherical effect of the earth has to be considered.
How much truth in such thinkings?
My case is that I only care about distances within a city, and such distances does not need to be precise at all, I just need a value so that I can do the comparison. In this case, I think even for super big cities, the spherical effect of the earth is neglectable, and even it is not, it won’t affect my distance comparison, I believe.
Hi @MBee and welcome to MongoDB community forums!!
The 2d indexes and the 2d sphere indexes both work on geospatial data and are used for different represenattion of geospatial data.
The 2d indexes perform on plane geometry, like a map, whereas the 2d sphere indexes works on sphere such as Earth’s surface.
I’m certainly not sure which geographic index performs better, but you can easily test it in your own development or staging environment. Please make sure to run the tests a few times and average the results for a fair comparison.
However, since the spherical index covers the spherical aspect of earth.
2d indexes are fine for locating things that are likely to be close by, such as the nearest bus stop or supermarket, but if you want to plot an aircraft flight, or a route to the next state then the results might deviate significantly from reality.
A 2dsphere index recognises all the polygons above as being the same size. 2dsphere indexes are good for determining more widely spaced items such as the nearest international airport etc.
For your use case specifically, if you are wondering which indexes you should go ahead with, the recommendation would surely be to use the 2d sphere index as starting in MongoDB version 3.2, GeoJSON geometries indexed at a finer gradation, improving performance of 2dsphere index queries over smaller regions.