Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2dsphere vs 2d index: which is "better"/faster?

Ι have a db where my documents are only Points. I consider adding a geospatial index. So I can either choose from a 2dsphere and a 2d one.

MongoDB.org has:

2dsphere indexes support:

 - Calculations on a sphere
 - Both GeoJSON objects and legacy coordinate pairs
 - A compound index with scalar index fields (i.e. ascending or
   descending) as a prefix or suffix of the 2dsphere index field

2d indexes support:

 - Calculations using flat geometry 
 - Legacy coordinate pairs (i.e.,    geospatial points on a flat
   coordinate system)  
 - A compound index with    only one additional    field, as a suffix of
   the 2d index field

However since all my documents are points I can either have one the options below in my schema without much difference.

for 2dsphere:

location : { 
      type : "Point" ,
      coordinates : [10,45] 
}

or for 2d index:

location : [10,45]

My question comes down to which one is faster? I really do not have a clue how to measure it.

The question assumes that I only want to query a square box of data and do not care for complex polygon searches: Either with $box which is only supported by the 2d index (if I read correctly) or with the $polygon method of $geoWithin supported by both indexes.

like image 392
Diolor Avatar asked Jan 10 '14 21:01

Diolor


1 Answers

In addition to the research you have already done, just wanted to add another difference I have come across between these two indexes.

By default, a 2d index on legacy coordinate pairs uses 26 bits of precision, which is roughly equivalent to 2 feet or 60 centimeters of precision using the default range of -180 to 180. Precision is measured by the size in bits of the geohash values used to store location data. You can configure geospatial indexes with up to 32 bits of precision.

db.<collection>.ensureIndex( {<location field> : "<index type>"} , { bits : <bit precision> } )

Source: MongoDB doc reference

like image 128
Viraj Avatar answered Nov 15 '22 12:11

Viraj