1
votes
$dm = $this->get('doctrine.odm.mongodb.document_manager');

$query = $dm->createQueryBuilder('MyBundle:Listing')
            ->select('title')
            ->field('coordinates')->geoNear(
                  (float)$longitude, 
                  (float)$latitude
            )->spherical(true);

$classifieds_array = $classifieds->toArray();

$data = array('success'=>true,'classifieds' => $classifieds_array, 
      'displaymessage' => $classifieds->count(). " Search Results Found");

Even though I am selecting just one field, for my result set, I am getting every thing back in collection along with title. Is this a bug?

NOTE: I commented out the ->field('coordinates')->geoNear((float)$longitude, (float)$latitude)->spherical(true) line and now the select seems to work. This is crazy.

1
I believe this is because of the way that a geoNear command runs: mongodb.org/display/DOCS/… Maybe doctrine is a bit confusing here in showing how the query is really formed? - Sammaye

1 Answers

0
votes

The geoNear command in MongoDB doesn't seem to support filtering result fields, according to the documentation examples. Only a query option is supported to limit matched documents.

In your case, it also looks like mixing up the geoNear() and near() builder methods in Doctrine. Since you're operating on the coordinates field, the appropriate syntax would be near(). geoNear() is a top-level method to tell the builder you wish to use the command, which doesn't require a field name since it uses the one and only geospatial index on the collection.

For usage examples, I would advise looking at the query and builder unit tests in the Doctrine MongoDB library.