1

I was wondering if there are any examples that consist on basic geolocation input and database retrieval of given inputs within a certain area, preferably the user's current location.

2 Answers 2

3

Here's a quick overview of geospatial queries in MongoDB:

  1. You have to insert location information in your collection via 2D array

    db.example.insert( {"location": [x,y], "category": "restaurant"})
    
  2. Set up a geospatial indexes on the collection by indicating the location field with "2d" option.

    db.example.ensureIndex( { "location" : "2d" } )
    

    Now the collection will have an index which all geospatial queries will be done against.

  3. To query for locations nearby a point (i.e. the user's current location), use the find() command with $near operand on the location field giving the point coordinates (i.e. 100, 100.)

    db.example.find( { "location": { $near : [100, 100] } } )
    
  4. To find users current location, you would need higher level application support to get the user's current information.

For more information:

Sign up to request clarification or add additional context in comments.

Comments

0

I don't have enough reputation to comment on the answer below, so had to post another answer:

ensureIndex() has been replaced with createIndex().

See the docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.