Issam Dakhlaoui
Issam Dakhlaoui

Reputation: 133

MongoDB query to find all places within a circle

Structure of Documents: (Facebook documents)

{
    "_id" : ObjectId("57a984222daf2339b1e06090"),
    "name" : "Arts /entertainment/nightlife",
    "location" : {
        "state" : "",
        "country" : "Nigeria",
        "latitude" : 6.45306,
        "city" : "Lagos",
        "longitude" : 3.39583,
        "street" : "no 15 petre agha street oke -afa isolo lagos",
        "zip" : "+234"
    },
    "id" : "519994548127995",
    "category" : "Arts/entertainment/nightlife",
    "category_list" : [
        {
            "name" : "Arts & Entertainment",
            "id" : "133436743388217"
        }
    ]
}

My question: What is the MongoDB query (using $geoWithin: { $center: [ [ <x>, <y> ] , <radius> ] } ) to find all the places that are within a circle?

Circle: Center: latitude = 48.856614 and longitude = 2.3522219000000177. Radius = 10 km.

I appreciate any answer that could help me.

Upvotes: 0

Views: 2372

Answers (2)

Issam Dakhlaoui
Issam Dakhlaoui

Reputation: 133

The answer of "notionquest" helped me to figure out the solution. But here is exactly my answer:

  • First Step: change the structure of the documents in order to have the location field like this:

    "location" : [x , y]` where x is the latitude, and y is the longitude.
    
  • Second Step: create the "2d" index on location field:

    db.geocircle.createIndex( { location : "2d" } );
    
  • Last step: the query is:

    db.geocircle.find(
               { location: { $geoWithin: { $center: [ [ 48.856614,  2.3522219000000177], 0.089992801 ] } } }
    );
    

Important note: the radius of the circle in the query is in degrees, so I need to convert 10 km to degrees. Given that one degree is approximately 111.12 kilometers, we can do the math.

Cheers!

Upvotes: 4

notionquest
notionquest

Reputation: 39226

Slightly modify the document structure for geo-spatial data as below.

/* 1 */
{
    "_id" : ObjectId("57a984222daf2339b1e06090"),
    "name" : "Arts /entertainment/nightlife",
    "state" : "",
    "country" : "Nigeria",
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            6.45306, 
            3.39583
        ]
    },
    "city" : "Lagos",
    "street" : "no 15 petre agha street oke -afa isolo lagos",
    "zip" : "+234",
    "id" : "519994548127995",
    "category" : "Arts/entertainment/nightlife",
    "category_list" : [ 
        {
            "name" : "Arts &amp; Entertainment",
            "id" : "133436743388217"
        }
    ]
}

Create the "2dsphere" index on location field.

db.geocircle.createIndex( { location : "2dsphere" } );

Then, execute the find queries.

The following queries should find Lagos.

db.geocircle.find(
   { location: { $geoWithin: { $center: [ [ 6.45306, 3.39583], 10 ] } } }
);

db.geocircle.find(
   { location: { $geoWithin: { $center: [ [ 6.45310, 3.39590], 10 ] } } }
);

This query shouldn't find Lagos.

db.geocircle.find(
   { location: { $geoWithin: { $center: [ [ 16.45310, 3.39590], 10 ] } } }
);

Upvotes: 0

Related Questions