Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.94k stars 3.84k forks source link

Multiple geonear queries #7485

Closed kkrishnan90 closed 5 years ago

kkrishnan90 commented 5 years ago

Hey guys ! Thanks a ton for such an awesome library ! I have a simple scenario and not sure if mongoose supports such query on geoJSON data.

I have a start and end location and a couple of more location objects as stop points intermediary to the start and end location geoJSON.

I have been thinking a lot about how to query multiple locations with geoNear or say near method through which when I match my documents based on start and end location if it matches.

Can someone explain on this please ? Thanks to all in advance.

P.S. I have searched all over the internet(stackoverflow especially) and I haven't found solution to this specific problem. So expecting some wizards guidance :stuck_out_tongue_winking_eye:

vkarpov15 commented 5 years ago

I'm not sure I understand the question, can you provide some specific examples of what your geojson objects look like?

Here's a couple relevant links: https://mongoosejs.com/docs/geojson.html http://thecodebarbarian.com/80-20-guide-to-mongodb-geospatial-queries

kkrishnan90 commented 5 years ago

Basically, I have the follow json schema

{
    driver_name:String,
    start_location:{type:Point, coordinates:[]}
    end_location:{type:Point, coordinates:[]}
    start_date:Date,
    start_time:Date,
    seats:Number
}

Now a let's say, I have a rider who searches for a trip (inputs a start and end location). Now I have to query all drivers who match the rider's search location (both start and end point as given). At this point, I need to query a geojson (driver's schema) based on both start and end location of another geojson (rider's schema).

Sorry, I hope I'm clear to you now ? If not, let me know. I shall explain a bit more on this.

vkarpov15 commented 5 years ago

Is Point a custom type? I suspect not, I'm guessing your schema definition should be:

start_location: { type: { type: 'string' }, coordinates: ['number'] }
kkrishnan90 commented 5 years ago

Yeah, that's fine on the schema part. Can you show some snippet to query multiple location based geoNear ? Like say, geoNear based on both start and end location.

vkarpov15 commented 5 years ago

@kkrishnan90 I'm not sure I understand your question, can you please clarify?

kkrishnan90 commented 5 years ago

Hi @vkarpov15 , sorry for such a delayed response. The objective of my mongoose query is to find trips that are matching close to the start and end location. For example, a driver creates a trip from location A to B .. now when a passenger is searching for a trip, I need to query based on the location matching or closest location. So when a passenger is searching by his start and destination location, I would like to query the database based on the closest match of both start and destination of trips created by driver.

vkarpov15 commented 5 years ago

@kkrishnan90 To support this query, I'd create a separate collection called TripPoint and create separate documents for trip start and trip end. That would make this query much simpler and faster.

GopalakrishnanP895 commented 2 years ago

@kkrishnan90 Did you find the solution? If yes please drop the snippet here or describe briefly how you solved. am also looking for the same scenario to query. Thanks