Loopback: near filter on object sub-field

只谈情不闲聊 提交于 2019-12-11 04:15:55

问题


I have a Model like this:

{
    name: 'My Favorite Shop',
    address:{
         location: [12.534 /* longitude */ ,41.9221/* latitude */]
    }
}

Documents are stored in a mongodb database, and thus I'd like to query them like:

//assuming shops to be the name of the containing model
app.models.shops.find({
    where: {
        'address.location': {lat: 13, lng: 40}
    }
}, callback);

Problem is this query would not retrieve any result. I guess it's because of the nature of location field, which is nested into address, but I couldn't verify that.

Any idea?

Thanks

UPDATE: it came out I forgot to set the enableGeoIndexing property in datasource definition. Once done, the query is retrieving the following error:

 {"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}

回答1:


I finally found the problem. A 2d index was applied on the address.location field, but Loopback (seems to) only support geoquery over 2dSphere indexes. Thus, I solved by adding a 2dSphere index over such field.



来源:https://stackoverflow.com/questions/40508291/loopback-near-filter-on-object-sub-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!