Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3634826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:49:39+00:00 2026-05-19T00:49:39+00:00

In the docs, it says that by default, Mongo treats it as lat/long: By

  • 0

In the docs, it says that by default, Mongo treats it as lat/long:

By default, the index assumes you are
indexing latitude/longitude and is
thus configured for a [-180..180]
value range.

So, let’s say I have this in my document:

post['loc'] = [ -40.234, 56.222]
db.mycollection.save(post)

I ensure my index:

db.mycollection.ensureIndex({"loc":"2d"})

Now, how do I execute the following in Mongo?

  • Give me all documents that are within 5 miles of a certain lat/long
  • Give me all documents within 400 meters of a certain lat/long
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:49:40+00:00Added an answer on May 19, 2026 at 12:49 am

    Use $near in conjuction with $maxDistance:

    db.mycollection.find({loc: {$near: [50, 50], $maxDistance: 5}})
    

    The unit of $maxDistance is the same as the loc field, i.e. degrees. A degree is approximately 69 miles or 111 km at the equator, if I remember correctly (but it is less at other latitudes, the exact distance is hard to calculate).

    To get more information about the returned locations, you can use the geoNear command, which will tell you the distance of all returned collections, among other things. See http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-geoNearCommand

    I think you can also use $geoWithin to solve your problem:

    db.mycollection.find({loc: {$geoWithin: {$center: [[50, 50], 5/3959]}}})
    

    This should find all locations within the circle centered at (50, 50) with a radius of 5/3959 degrees (i.e. 5 miles).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sometimes signals in django are triggered twice. In the docs it says that a
In the Apple docs it says that JavaScript execution time is limited to 10
based on the docs for knockout.mapping.js it says that properties of a data object
I read http://code.google.com/speed/page-speed/docs/caching.html . It says that proxy servers may cache cookies. I need
Java Docs says that, putIfAbsent is equivalent to if (!map.containsKey(key)) return map.put(key, value); else
Python docs says that slicing a list returns a new list. Now if a
The docs says that ancestor will apply a filter of given ancestor being somewhere
The official docs says that smarty has 'while' support . However I do in
The docs for Object.Equals says that implementors must return false if the parameter is
The following article http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT3.html says that XPATH considers the following to be nodes: Root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.