0

How Could i get longitude and lattitude using mongodb query from mongo collection based on disatnce and certain coordinates.

{
    "_id" : ObjectId("5a559b13ae201d0c05cb6f6a"),
    "id" : "99623",
    "city" : "Wasilla",
    "cnty" : "Matanuska Susitna",
    "cntyFips" : null,
    "st" : "AK",
    "stName" : "Alaska",
    "areaCode" : "907",
    "lat" : 61.5816,
    "long" : -149.4393,
    "rgn" : "West",
    "__v" : 0 
}

in this document "lat" is for latitude and "long" is for longitude.

so basically i have to get all ids based on present id lat and long which comes in a particular miles distance range.

2 Answers 2

1

Mongo supports GeoJSON which should allow you to perform basic queries easily on these kind of data. Examples include $geoWithin for your particular use-case.

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

Comments

0

We need a little more information on the structure of the documents that you are querying, but here is a simple example of the query available in the Mongo documentation here.
It's worth noting that you require a 2dsphere index for location data defined as GeoJSON points in order for this to be effective.

 {
   $nearSphere: {
      $geometry: {
         type : "Point",
         coordinates : [ <longitude>, <latitude> ]
      },
      $minDistance: <distance in meters>,
      $maxDistance: <distance in meters>
   }
 }

5 Comments

i have a document which contains latitude and longitude and id as zipcode. { "_id" : ObjectId("5a559b13ae201d0c05cb6f6a"), "id" : "99623", "city" : "Wasilla", "cnty" : "Matanuska Susitna", "cntyFips" : null, "st" : "AK", "stName" : "Alaska", "areaCode" : "907", "lat" : 61.5816, "long" : -149.4393, "rgn" : "West", "__v" : 0 } in this document lat is for latitude and long is for longitude. so basically i have to get all ids based on present id lat and long which comes in a particular miles distance range.
Is it possible to update the structure of your data or are you locked into the format? You could write a complex query that could get the information but it would be best and fastest to get your data coordinates into a GeoJSON format.
thnx @Genthe i managed my document structure in GeoJSON format .
So it's all working for you now or do you still need assistance?
That's great. Glad you are sorted.

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.