MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Is that possible to use geoNear twice?

I want to make a mongodb query to search companies by applying following filter.

{
    myLocation: [lng1, lat1], // That is to get distance from current location to companies on result.
    region: [lng2, lat2], //That is to get companies within a radius in this region
    radius: 10000,
    tags: ["tag1", "tag2"],
    sort: "asc" // to sort by nearest from current location.
}

I want to make a mongodb query to search companies by applying following filter.

{
    myLocation: [lng1, lat1], // That is to get distance from current location to companies on result.
    region: [lng2, lat2], //That is to get companies within a radius in this region
    radius: 10000,
    tags: ["tag1", "tag2"],
    sort: "asc" // to sort by nearest from current location.
}

I thought it needs to use $geoNear twice and made an aggregation like this.

{
    $geoNear: {
        near: {
            type: "Point",
            coordinates: myLocation
        },
        distanceField: 'distance1',
        maxDistance: radius,
        spherical: true,
    },
    $match: { tags: tags },
    $geoNear: {
        near: {
            type: "Point",
            coordinates: region
        },
        distanceField: 'distance2',
        spherical: true,
    },
    $sort: {"distance2": 1}
}

but it occurs an error - $geoNear is only valid as the first stage in a pipeline.

Is there any way to make a query to work this filter.

Appreciate your help.