MongoDB中的复合地理空间索引未按预期工作

时间:2014-06-03 13:46:35

标签: mongodb indexing geospatial compound-index

集合nodesWays具有以下索引:

> 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           
        }                                            
] 

现在,以下两个查询应该返回相同的结果,但它们不会。 我想要最近的10家餐厅到指定点。 第一个查询是如何工作的,第二个查询不是按预期工作。

这两个查询之间的唯一区别是第一个使用geo_2dsphere-Index 第二个查询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" }

我对第二个索引的目标是: 选择所有餐厅 然后使用nearSphere-Operator根据指定点的距离对它们进行排序

Auf Wiedersehen

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试将地理位置放在索引中。