Indexing

Indexing is a means by which a database management system sorts the data such that it is easier, and much faster, to find and work with.

MongoDB has a form of indexing which is specific to geo spatial information. This is called 2DSphere.

2DSphere: The Science

A 2DSphere index is used by MongoDB to organize spatial data in a meaningful manner.

According to the documentation, this index makes use of a C++ Google library to index geometrical data.

First there is a set of cells which are known beforehand cover the earth exactly. Secondly, to identify a certain geometry, a set of cells is selected which cover this geometry in all its fullness. This is called a “covering.”

https://www.mongodb.com/blog/post/new-geo-features-in-mongodb-24

Depending on the size of the geometry in question, a finer “granularity” can be used to define this covering. This means that one has a covering, but the cell sizes are smaller.

Furthermore these coverings are placed into bigger cells. This then forms a part of a normal B-Tree index which is used in many other indexing algorithms.

2DSphere: Practically

You can create this amazing index with the MongoDB compass program by going to the index tab and selecting create index. The type is 2dshpere and the property used is “geometry.”


On the other hand, if you prefer the command line, the following command does exactly the same thing, namely creating a 2dsphere index on the geography field of collection SomeCollection:

db.SomeCollection.ensureIndex({"geometry":"2dsphere"})

Leave a comment

Design a site like this with WordPress.com
Get started