# Diagnosing Incorrect Results in a MongoDB Geospatial Radius Query

DevFeed: [Diagnosing Incorrect Results in a MongoDB Geospatial Radius Query](<https://devfeed.tech/articles/are-you-using-the-right-mongo-geospatial-query-19163.md>)

Original publisher: [Read original article](<https://artsy.github.io/blog/2023/02/10/mongo-geospatial-queries/>)

Published: 2023-02-10T00:00:00Z

Content type: article

Language: en

Sources: [Artsy](<https://devfeed.tech/sources/artsy.md>)

Topics: [MongoDB](<https://devfeed.tech/topics/mongodb.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [API](<https://devfeed.tech/topics/api.md>), [data](<https://devfeed.tech/topics/data.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [code](<https://devfeed.tech/tags/code.md>), [data](<https://devfeed.tech/tags/data.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [geospatial](<https://devfeed.tech/tags/geospatial.md>), [gravity](<https://devfeed.tech/tags/gravity.md>), [json](<https://devfeed.tech/tags/json.md>), [mongodb](<https://devfeed.tech/tags/mongodb.md>), [rails](<https://devfeed.tech/tags/rails.md>)

## AI overview

This article investigates why a MongoDB geospatial query omitted a gallery from Los Angeles exhibition listings. By widening the radius and visualizing the results, it finds that points near the left and right edges of the circle were incorrectly omitted.

## Source excerpt

We recently got a report from one of our galleries in the Los Angeles area that they weren't showing up on our Los Angeles exhibition listings. I fielded the report and right away confirmed: when we asked our core API for /shows?near=<los angeles coordinates>, sure enough this gallery partner didn't make the cut. Turns out they are based in Santa Monica, a separate and neighboring municipality. They must not be within the 25km radius that we use by default for these sorts of queries. Case closed. Or so I thought. After some back and forth with our partner I decided to investigate more thoroughly, this time using some tricks of the trade from my other life outside of Artsy. Casting a wider net If there was something wrong with our 25km radius query, I wanted to start by casting a wider net and visualizing the results. I consulted our Rails application code to find the query logic in question, and then issued the same query directly to MongoDB. Something like the following query (simplified for clarity): // a $geoWithin $center query db.events.find({ coordinates: { $geoWithin: { $center: [[-118.24, 34.05], 25 / 111.32], }, }, }) In the query above we are asking MongoDB to give us all events within a 25km radius around the point 34.05°N, 118.24°W which we have designated as a central point within Los Angeles. For our purposes in this post we can consider "events" ~= "exhibitions" ~= "shows." We are not asking for the 25km radius directly, but rather converting it into an equivalent amount of geographic degrees by using a conversion factor of 1° ~= 111.32 kilometers, a factor which is true at enough the equator. I modified the above query to cast a 50km net in order to see if there were some edges cases that needed scrutiny. Taking the resulting JSON response, I fired up Placemark, my favorite new tool for wrangling geospatial data. (Incidentally I recommend reading Tom Macwright's recent reflection on creating Placemark as a bootstrapped indie developer.) All shows within