17

I have a set of documents each of which contain a point in 3-space stored in a MongoDB collection. MongoDB currently has Geospatial Indexes only for 2-space. Is there a way of leveraging the Geospacial Index to do similar queries on 3-space data?

John F. Miller
  • 26,961
  • 10
  • 71
  • 121
  • You mean instead of x you want x, y, z? – Justin Thomas Mar 25 '11 at 17:45
  • 4
    +1 - I didn't even know Mongo had a Geospatial Index.. – NG. Mar 25 '11 at 17:46
  • It has a spherical and box model for geospatial, the box model isn't as useful to hard core GIS types, but fun to play with for location apps. – Justin Thomas Mar 25 '11 at 17:49
  • Yes, Mongodb has Geospacial Indexes for flat 2d and spherical 2d. (http://www.mongodb.org/display/DOCS/Geospatial+Indexing) If you are mapping points on the surface of the Earth it can build a Geospacial Index and do fast proximity queries, unfortunately my data has a third dimension. – John F. Miller Mar 25 '11 at 17:57
  • 3
    The feature request for n dimensional geospatical support in mongo is http://jira.mongodb.org/browse/SERVER-691 – Sridhar Mar 25 '11 at 18:22
  • @Sridhar: Thanks, it is nice to know it is "in the works!" An interesting side note, I would love to see indexing for n <= 4, and it may well be easier to just impliment n-space generally, but as n increases the value of the index decreases (I think as n! but don't quote me). Search for "curse of dimensionality" for the whole story. – John F. Miller Mar 25 '11 at 18:32

1 Answers1

1

You could kludge it by compressing one of the dimensions, but you would lose half your precision.

Say if they were 64 bit keys and you wanted to store three 32 bit coordinates: [(x << 32) + y, z]

Heck, you can even interleave three and store it in one key:

xxx

yyy

zzz

xyzxyzyz

Chad Brewbaker
  • 2,523
  • 2
  • 19
  • 26
  • 1
    I think this would mess up the concept of "near" that the Geospacial index is used for, would it not? – John F. Miller Mar 31 '11 at 05:07
  • Yes and no. If you set up the low bits of the smashed dimension with the variable which is most random you should be fine. The second trick of interleaving bits works great to smoosh an aribitrary number of dimensions down into one. – Chad Brewbaker Apr 05 '11 at 00:44
  • I don't really understand this. How would this actually work in MongoDB JavaScript shell code? Could you provide an example of how to define the index & a sample query? Thanks! Also, how would you do this: http://stackoverflow.com/questions/6003943/mongodb-find-most-recent-closest-posts-limit-20 – ma11hew28 May 15 '11 at 03:06
  • 1
    For exact queries -- yes, but then you don't need spacial indexing -- regular indexing will work fine, and you can do multicolumn indexes, so no kludge necessary. For near queries -- you've effectively thrown away the y dimension (worse, introduced noise in the x dimension) so you again might as well skip the kludge and just spacially index [x, z]. – Aaron Dec 06 '11 at 23:20