2
votes

I have a list of Points of Interest, say, restaurants, theatres, gardens, etc. saved in a Mongo database. Then, I have an application that tracks the user's movements in a city and keeps gathering the location intermittently. If I find that a person is "near" a point of interest, I need to send her/him a notification. "Nearness" for a POI is a variable quantity - for a restaurant it could be 100m, for a theatre it could be 200m, for a garden it could be 20m, etc.

Each POI is, of course, saved as a separate document. Now, is there a way to fire a query on MongoDB with the lat/lon of a person and retrieve the list of POIs nearby?

Say, my documents are like this:

{ "type": "restaurant", "name": "Binny's", "loc": [-122.10396, 37.45093], "maxDist": 100 }

{ "type": "museum", "name": "Ripley's", "loc": [-122.24546, 37.48251], "maxDist": 500 }

I need to fire a query that gives me the list of POIs nearby (not more than 'maxDist' metre away from the POI) for a given lat/lon value, say, [-122.12098,37.46001].

Is there a way to construct a query to achieve this? If I have to alter the document structure a little bit, that's fine.

Thanks in advance.

1

1 Answers

0
votes

Use $near. It will allow you to query the position of the user and all locations that are within a specific distance.