Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MongoDB has <a href="http://www.mongodb.org/display/DOCS/Geospatial+Indexing" rel="nofollow">support for spatial indexes built-in</a>, so all you'd need to do is load your points using the correct format, create the spatial index, and then run your queries.</p> <p>For a quick example, I loaded the center points for all 50 states in the mongo shell:</p> <pre><code>&gt; db.places.ensureIndex({loc: "2d"}) &gt; db.places.save({name: "AK", loc: {long: -152.2683, lat: 61.3850}}) &gt; db.places.save({name: "AL", loc: {long: -86.8073, lat: 32.7990}}) &gt; db.places.save({name: "AR", loc: {long: -92.3809, lat: 34.9513}}) &gt; db.places.save({name: "AS", loc: {long: -170.7197, lat: 14.2417}}) &gt; ... </code></pre> <p>Next, to query for the <strong>6 nearest points to a given location</strong>:</p> <pre><code>&gt; db.places.find({loc: { $near: {long: -90, lat: 50}}}).limit(6) {"name" : "WI", "loc" : { "long" : -89.6385, "lat" : 44.2563 } } {"name" : "MN", "loc" : { "long" : -93.9196, "lat" : 45.7326 } } {"name" : "MI", "loc" : { "long" : -84.5603, "lat" : 43.3504 } } {"name" : "IA", "loc" : { "long" : -93.214, "lat" : 42.0046 } } {"name" : "IL", "loc" : { "long" : -89.0022, "lat" : 40.3363 } } {"name" : "ND", "loc" : { "long" : -99.793, "lat" : 47.5362 } } </code></pre> <p>Next, to query for <strong>all points within 10km of a given location</strong>. Since I'm calculating the nearest states, I'll use 888km (which is approximately 8 degrees of latitude):</p> <pre><code>&gt; db.places.find({loc: { $near: {long: -90, lat: 50}, $maxDistance: 8}}) {"name" : "WI", "loc" : { "long" : -89.6385, "lat" : 44.2563 } } {"name" : "MN", "loc" : { "long" : -93.9196, "lat" : 45.7326 } } </code></pre> <p>Since <a href="http://en.wikipedia.org/wiki/Latitude#Degree_length" rel="nofollow">one degree of latitude is <strong>approximately</strong> 111.12km</a>, you'd use a <code>$maxDistance: 0.08999</code> to represent 10km for your application.</p> <p><strong>Updated</strong> By default MongoDB assumes an "idealized flat earth model" but this results in inaccuracies since longitude lines converge at the poles. <a href="http://www.mongodb.org/display/DOCS/Geospatial+Indexing" rel="nofollow">MongoDB versions 1.7+ support spherical distance calculations</a>, which provides the increased precision.</p> <p>Here is an example of running the above query using spherical distance. the <code>maxDistance</code> is in radians, so we need to divide by the earth's average radius:</p> <pre><code>&gt; db.runCommand({geoNear: "places", near: [-90, 50], spherical: true, maxDistance: 800/6378}); (summarizing results as they're too verbose to include) "MN" dis: 0.087.. "WI" dis: 0.100.. "ND" dis: 0.120.. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload