Hey MongoDB experts
I am trying to achieve some query results using MongoDB various location features ($near, $geoNear and more).
I have this mongoose model with geoJSON type.
const geoSchema = new Schema({
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
},
});
const pickupSchema = new Schema({
geo_location_from: geoSchema,
geo_location_to: geoSchema,
});
pickupSchema.index({ geo_location_from: '2dsphere' });
pickupSchema.index({ geo_location_to: '2dsphere' });
What I am trying to achieve is near by location of the event.
I have main pickup event from A to B, and as displayed in the image I have Latitude and Longitude of the all location. Now I am trying to query all of those events object from the db where event geo_location_from is near by Location A (example: A1, A2, A3 ) and geo_location_to is near by Location B ( B1, B2 ).
Here is something I did, which is not right. I am not 100% sure.
Pickup.find(
{
$and: [{
geo_location_from: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_from, args.latitude_from],
},
},
},
}, {
geo_location_to: {
$near: {
$maxDistance: 1000,
$geometry: {
type: 'Point',
coordinates: [args.longitude_to, args.latitude_to],
},
},
},
}],
},
)
Some of my try ended up giving various kind of errors. like Too many geoNear expressions and many more.
Anybody has any good solution to this kind of problem ?