0

the collection nodesWays has the following indexes:

> db.nodesWays.getIndexes()                          
[                                                    
        {                                            
                "v" : 1,                             
                "key" : {                            
                        "_id" : 1                    
                },                                   
                "name" : "_id_",                     
                "ns" : "h595.nodesWays"              
        },                                           
        {                                            
                "v" : 1,                             
                "key" : {                            
                        "amenity" : 1,               
                        "geo" : "2dsphere"           
                },                                   
                "name" : "amenity_1_geo_2dsphere",   
                "ns" : "h595.nodesWays",             
                "2dsphereIndexVersion" : 2           
        },                                           
        {                                            
                "v" : 1,                             
                "key" : {                            
                        "geo" : "2dsphere"           
                },                                   
                "name" : "geo_2dsphere",             
                "ns" : "h595.nodesWays",             
                "2dsphereIndexVersion" : 2           
        }                                            
] 

Now the following two queries should return the same result, but they don't. I want the nearest 10 restaurants to the specified point. The first query is working how it should be, the second is not working like intended.

The only difference between these two queries is that the first one uses the geo_2dsphere-Index and the second query the amenity_1_geo_2dsphere-Index.

> db.nodesWays.find(
{
    geo:{
        $nearSphere:{
            $geometry:{
                type: "Point", coordinates:  [9.7399777,52.3715156]
            }
        }
    }, "amenity":"restaurant", 
        name: {$exists: true}
}, {id:1, name:1}).hint( "geo_2dsphere" ).limit(10)

{ "_id" : ObjectId("53884860e552e471be2b7192"), "id" : "321256694", "name" : "Masa" }
{ "_id" : ObjectId("53884860e552e471be2b7495"), "id" : "323101271", "name" : "Bavarium" }
{ "_id" : ObjectId("53884862e552e471be2ba605"), "id" : "442496282", "name" : "Naxos" }
{ "_id" : ObjectId("53884860e552e471be2b7488"), "id" : "323101189", "name" : "Block House" }
{ "_id" : ObjectId("53884878e552e471be2d1a41"), "id" : "2453236451", "name" : "Maestro" }
{ "_id" : ObjectId("53884870e552e471be2c8aab"), "id" : "1992166428", "name" : "Weinstube Leonardo Ristorante" }
{ "_id" : ObjectId("53884869e552e471be2c168b"), "id" : "1440320284", "name" : "Altdeutsche küche" }
{ "_id" : ObjectId("53884861e552e471be2b88f7"), "id" : "353119010", "name" : "Mövenpick" }
{ "_id" : ObjectId("5388485de552e471be2b2c86"), "id" : "265546900", "name" : "Miles" }
{ "_id" : ObjectId("53884863e552e471be2bb5d3"), "id" : "532304135", "name" : "Globetrotter" }

> db.nodesWays.find(
{
    geo:{
        $nearSphere:{
            $geometry:{
                type: "Point", coordinates:  [9.7399777,52.3715156]
            }
        }
    }, "amenity":"restaurant", 
        name: {$exists: true}
}, {id:1, name:1}).hint( "amenity_1_geo_2dsphere" ).limit(10)

{ "_id" : ObjectId("53884875e552e471be2cf4a8"), "id" : "2110027373", "name" : "Schloßhof Salder" }
{ "_id" : ObjectId("5388485be552e471be2aff19"), "id" : "129985174", "name" : "Balkan Paradies" }
{ "_id" : ObjectId("5388485be552e471be2afeb4"), "id" : "129951134", "name" : "Asia Dragon" }
{ "_id" : ObjectId("53884863e552e471be2ba811"), "id" : "450130115", "name" : "Kings Palast" }
{ "_id" : ObjectId("53884863e552e471be2ba823"), "id" : "450130135", "name" : "Restaurant Montenegro" }
{ "_id" : ObjectId("53884877e552e471be2d053a"), "id" : "2298722569", "name" : "Pizzaria Da-Lucia" }
{ "_id" : ObjectId("53884869e552e471be2c152e"), "id" : "1420101752", "name" : "Napoli" }
{ "_id" : ObjectId("5388485be552e471be2b0028"), "id" : "136710095", "name" : "Europa" }
{ "_id" : ObjectId("53884862e552e471be2ba5bc"), "id" : "442136241", "name" : "Syrtaki" }
{ "_id" : ObjectId("53884863e552e471be2ba763"), "id" : "447972565", "name" : "Pamukkale" }

My goal with the second index is to: select all restaurants then use the nearSphere-Operator to sort them in regards to the distance from the specified point

Auf Wiedersehen

Andre
  • 1,249
  • 1
  • 15
  • 38
  • I'd appreciate if you could provide some feedback on others' answers/suggestions, whether it makes sense or have points to improve on. It helps re-enforce the correct ideas and clears bad ideas. – Lee Jun 03 '14 at 14:49

1 Answers1

0

I think you should try to put the geolocation first in the index.

Lee
  • 2,874
  • 3
  • 27
  • 51
  • That works, but is slower than the geo_2dsphere-Index. I created an index with geo-Field first, amenity second for the nodesWays-Table and than executed the query without a hint. The result was that MongoDB choose the amenity_1_geo_2dsphere-Index and retruned the wrong result. – Andre Jun 04 '14 at 06:43
  • This seems to me like a bug.? – Andre Jun 04 '14 at 06:43