I'm running an aggregate on my 'Users' collection to assign a list of cities that are within some radius of the users coordinates.
It seems that theres no way to assign let variables to the maxDistance field as this can only accept raw numbers.
Is there another way I can do the same kind of calculation not using $geoNear, or $geoWithin as both of these have the same limitations.
$lookup: {
from: 'cities'
let: {
userCoordinates: '$coordinates',
userSearchRadius: '$searchRadius'
},
pipeline: [
{
$geoNear: {
near: {
type: "Point",
coordinates: '$$userCoordinates'
},
distanceField: 'distance',
maxDistance: '$$userSearchRadius', // Issue here
spherical: true,
}
}
],
as: 'nearbyCities'
}
A workaround to your issue would be adding a
$matchstage after the$geoNearstage on the calculateddistancefield.Mongo Playground