回送:对附近物品子场滤波器
问题描述:
我有这样的型号:回送:对附近物品子场滤波器
{
name: 'My Favorite Shop',
address:{
location: [12.534 /* longitude */ ,41.9221/* latitude */]
}
}
文档存储在MongoDB数据库,因此我想对它们进行查询,如:
//assuming shops to be the name of the containing model
app.models.shops.find({
where: {
'address.location': {lat: 13, lng: 40}
}
}, callback);
问题是此查询不会检索任何结果。我想这是因为location
字段的性质,它嵌套在address
中,但我无法验证。
有什么想法?
感谢
UPDATE: 它出来我忘了设置数据源定义的enableGeoIndexing
财产。完成后,查询将检索以下错误:
{"name":"MongoError","message":"Unable to execute query: error processing query: ns=acidata-dev.pos limit=25 skip=0\nTree: GEONEAR field=address.location maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query","$err":"Unable to execute query: error processing query: ns=acidata-dev.pos limit=25 skip=0\nTree: GEONEAR field=address.location maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query","code":17007}
答
我终于找到了问题。在address.location
字段上应用了2d
索引,但Loopback(似乎)仅支持对2dSphere
索引的地理查询。因此,我通过在这样的领域增加一个2dSphere
索引来解决。