Skip to content

Instantly share code, notes, and snippets.

@shanestillwell
Created January 17, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanestillwell/1627491 to your computer and use it in GitHub Desktop.
Save shanestillwell/1627491 to your computer and use it in GitHub Desktop.
Geospatial issues
<?php
namespace Application;
/**
* @Document(collection="events")
* @Index(keys={"coordinates"="2d"})
* @HasLifecycleCallbacks
*/
class Event extends Base
{
const STATUS_LIVE = 'live';
const STATUS_CANCELLED = 'cancelled';
/**
* @Id
* @var string
*/
protected $id;
/**
* @var string
* @EmbedOne(targetDocument="Application\Coordinates")
*/
protected $coordinates;
/**
* @Distance
*/
public $distance;
}
?>
<?php
namespace Application;
/**
* @EmbeddedDocument
*/
class Coordinates extends Base
{
/**
* @Float
*/
protected $latitude;
/**
* @Float
*/
protected $longitude;
}
?>
<?php
/**************** Client Code to get results *****************/
// All these give zero results
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near('[50,60]')->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near('50,60')->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near(50,60)->getQuery()->execute();
$places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near(array(50,60))->getQuery()->execute();
/**
//The Index is in place
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "pfcd_dev.events", "name" : "_id_" }
{ "v" : 1, "key" : { "coordinates" : "2d" }, "ns" : "pfcd_dev.events", "name" : "coordinates_" }
// When I run this command
>db.events.find( { coordinates: { $near : [50,50] }} )
{ "_id" : ObjectId("4f14f363b2a87a7981000000"), "created_on" : ISODate("2012-01-17T04:04:50Z"), "updated_on" : ISODate("2012-01-17T04:04:50Z"), "coordinates" : { "latitude" : 50.700201, "longitude" : 60.470551 }, "status" : "live" }
/*********************************************************************************************
THE SOLUTION
Using this more specific query produced the results I was looking for
********************************************************************************************************/
$places = $this->dm->createQueryBuilder('\Application\Event')
->field('coordinates.latitude')->near(50)
->field('coordinates.longitude')->near(60)
->getQuery()->execute();
@avalanche123
Copy link

Is there a typo?

db.events.find( { coordinates: { $near : [50,50] }} )

should be

db.events.find( { coordinates: { $near : [50,60] }} )

@shanestillwell
Copy link
Author

No, because either way [50,60] or [50,50] the result is still the same. Doctrine returns no results... MongoShell returns numerous results.

I should also note that the following command

db.runCommand( { geoNear : "events" , near : [50,60], num : 1 } )

Returns

    {
        "ns" : "pfcd_dev.events",
        "near" : "1100110001001110110001001110110001001110110001001110",
        "results" : [
            {
                "dis" : 0.8436229513248203,
                "obj" : {
                    "_id" : ObjectId("4f14f363b2a87a7981000000"),
                    "created_on" : ISODate("2012-01-17T04:04:50Z"),
                    "updated_on" : ISODate("2012-01-17T04:04:50Z"),
                    "coordinates" : {
                        "latitude" : 50.700201,
                        "longitude" : 60.470551
                    },
                    "status" : "live"
                }
            }
        ],
        "stats" : {
            "time" : 3,
            "btreelocs" : 0,
            "nscanned" : 3,
            "objectsLoaded" : 2,
            "avgDistance" : 0.8436229513248203,
            "maxDistance" : 0.8436337898378685
        },
        "ok" : 1
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment