mongodb - Mongo Get all the documents within 10 miles of location specified -


i have 2 collection serviceprovider , parents.

serviceprovider(_id, serviceproviderid, ...)

parents( _id, id, location:[ lat, long ], ... )

serviceproviderid of serviceprovider collections linked id of parents collections.

i want service providers within 10 miles of locations.

so want join serviceprovider , parents collection , list serviceprovider within 10 miles of location.

i have tried following query:

db.serviceprovider.aggregate([    {         $lookup:         {             from: "parents",             localfield: "serviceproviderid",             foreignfield: "id",             as: "serviceproviderarr"         }    },    {       $match: { "serviceproviderarr": { $ne: [] } }    } ]) 

it gives me following result:

{     "_id" : objectid("577cc2ce588aec59178b4567"),     ...     "serviceproviderarr" : [         {             "_id" : objectid("577cbe33588aecf6168b45c6"),             "id" : "193",             ...             "location" : {                 "lat" : "40.75368539999999",                 "long" : "-73.9991637"             }         }     ] } 

if understand well, query might need:

db.parents.aggregate([     { $geonear: {                  near: { type: "point", coordinates: [ 0, 0 ] },                  spherical: true,                  distancefield: "dist.calculatedd",                  maxdistance: 10              }    },    { $lookup: {             from: "serviceprovider",             localfield: "id",             foreignfield: "serviceproviderid",             as: "serviceproviderarr"         }     } ]).pretty() 

you need change center point coordinates , max distance accordingly


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -