Search code examples
mongodbloopbackjsstrongloop

Loopback: near filter on object sub-field


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}

Solution

  • 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.