首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB中的复合GeoSpatial索引未按预期工作

MongoDB中的复合GeoSpatial索引未按预期工作
EN

Stack Overflow用户
提问于 2014-06-03 21:46:35
回答 1查看 350关注 0票数 0

集合nodesWays具有以下索引:

代码语言:javascript
复制
> 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。

代码语言:javascript
复制
> 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-运算符根据与指定点的距离对它们进行排序

Auf Wiedersehen

EN

回答 1

Stack Overflow用户

发布于 2014-06-03 22:13:29

我认为你应该试着把地理位置放在索引的第一位。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24016864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档