Skip to content

@shanestillwell /geospatial.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

Is there a typo?

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

should be

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

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
Something went wrong with that request. Please try again.