Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't add that sort of grouping constraint to your query (at least, with MongoDB 2.0.x) so you would have to iterate the results in your app code to pull out what you need.</p> <p>Have created an example below assuming that:</p> <ul> <li>it would be useful to keep track of the number of matches for a given name </li> <li>you only want to include details of the nearest match for a given name</li> <li>MongoDB <a href="http://www.mongodb.org/display/DOCS/Geospatial+Indexing/#GeospatialIndexing-geoNearCommand" rel="nofollow">geoNear()</a> sorts by distance so the first match should be the nearest</li> </ul> <p>This is using the JS shell:</p> <pre><code>// Hash to save results &gt; var cars = {} // Find closest cars to given geo point using geoNear &gt; db.runCommand( { geoNear: 'car', near : [50.93939251390,-114], num:5} ).results.forEach( function(doc) { if (cars[doc.obj.name]) { // Increment number of matches for this name cars[doc.obj.name]['matches'] = cars[doc.obj.name]['matches'] + 1; } else { // Closest match found doc.obj['matches'] = 1; doc.obj['distance'] = doc.dis; cars[doc.obj.name] = doc.obj; } } ) // Check the results &gt; cars { "Skoda" : { "_id" : ObjectId("501d108c9d2b5b2b5443712d"), "name" : "Skoda", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 }, "matches" : 2, "distance" : 0.0010000000000047748 }, "Skoda SUV" : { "_id" : ObjectId("501d108c9d2b5b2b5443712f"), "name" : "Skoda SUV", "affiliation" : "Skoda", "loc" : { "lon" : 50.93939251390387, "lat" : -113.999 }, "matches" : 1, "distance" : 0.0010000000000047748 }, "Benz" : { "_id" : ObjectId("501d108c9d2b5b2b5443712e"), "name" : "Benz", "affiliation" : "Skoda", "loc" : { "lon" : 51.93939251390387, "lat" : -113.999 }, "matches" : 1, "distance" : 1.0000005000037404 }, "Ford" : { "_id" : ObjectId("501d108c9d2b5b2b5443712a"), "name" : "Ford", "affiliation" : "Ford", "loc" : { "lon" : 45.93939251390387, "lat" : -113.999 }, "matches" : 1, "distance" : 5.000000099996134 } } </code></pre> <p>A common approach for this sort of search result would be to display all matches found in a list and also plot them on a map. Ideally your map view would support some sort of <a href="http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries#MarkerClusterer" rel="nofollow">Marker Clustering</a> to automatically group markers based on the user's selected zoom level.</p>
    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. This table or related slice is empty.
    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