Search code examples
phpmongodbgeojson

Unable to retrieve records from Mongodb for lat,long with $near query


I am very new to Geo-Spatial data search with mongodb. I need to retrieve the postcodes(resultant lat,long based) of UK which are near to the lat,long which i am providing to query. My mongodb query is as follows:

$mongoPostcode = MongoDbConnector::getCollection("postcodes");
$query = array(
'location' => array(
    '$near' => array(
        'latitude' => 57.14270109,
        'longitude' => -2.093014619,
        '$maxDistance' => 15 / 111.12 // 1 degree is approximately 111.12 kilometers. 15 km of 111.12 would be 15/111.12
    )
)
);

$nearByPostcodeData = $mongoPostcode->find($query);

My mongo's raw record entry looks like this:

{
"_id": {
"$oid": "56ced2e6cffa1906847b6868"
},
"postcode": "AB11 8RQ",
"location": {
"latitude": 57.13597762,
"longitude": -2.072114784
},
"timestamp": 1454601532,
"active_status": 1
}

But when i do search it returns me following error:

Fatal error: Uncaught exception 'MongoCursorException' with message 'localhost:27017: Unable to execute query: error processing query: ns=sampleproject.postcodes limit=0 skip=0 Tree: GEONEAR field=location maxdist=0.134989 isNearSphere=1 Sort: {} Proj: {} planner returned error: unable to find index for $geoNear query' in C:\xampp\htdocs\sampleproject\files\php\searchByPostCode.php:35 Stack trace: #0 C:\xampp\htdocs\sampleproject\files\php\searchByPostCode.php(35): MongoCursor->rewind() #1 {main} thrown in C:\xampp\htdocs\sampleproject\files\php\searchByPostCode.php on line 35

Is there anything missing in sample data modeling or is it a query problem?


Solution

  • You need to create a special index. 2D Sphere index

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

    Edit

    You can modify your collection to make a query with meters instead of radians.

    {
        "_id": { "$oid": "56ced2e6cffa1906847b6868" },
        "postcode": "AB11 8RQ",
        "location": {
            "type": "Point",
            "coordinates": [
                -2.072114784, // Longitude
                57.13597762 // Latitude
            ]
         },
         "timestamp": 1454601532,
         "active_status": 1
     }
    

    Your query will look like this:

    $query = array(
        'location' => array(
            '$near' => array(
                'latitude' => 57.14270109,
                'longitude' => -2.093014619,
                '$maxDistance' => 1000 // 1000 meters        )
            )
         )
    );
    

    This would require to reshape your entire collection. Details can be found here.