问题
I'm using DoctrineMongoDBBundle with Symfony2 and I've a problem with geocoordinates. This works fine but when the longitude is for example like that: 0.635467 the code doesn't work. I have more geocoordinates and only fails when it begins with 0. and the distance field is NULL.
This is my code:
$locations = $dm->createQueryBuilder('MyBundle:Location')
->field('id')->in($arrayIds)
->field('geocoordinates')
->geoNear($geocodes['lat'],$geocodes['lon'])
->getQuery()->execute()->toArray();
I'm following this link: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/geospatial-queries.html but with the geonear method.
回答1:
The geoNear()
query builder method is not intended to be used on a field. near()
is the builder method that would follow a field()
focus. You can see what both of these builder methods do in Builder.php within the doctrine/mongodb project. Note that geoNear()
changes the query type (similar to what update()
does). The query type is then checked in Query.php (follow the switch statement) and determines how we issue the query on the collection. Some are actual query operations, but things like map/reduce and geoNear are commands.
See if the following code works:
$dm->createQueryBuilder('MyBundle:Location')
->geoNear($geocodes['lat'],$geocodes['lon'])
->field('id')->in($arrayIds)
->getQuery()->execute()->toArray();
If not, please debug the values that Query.php passes to the Collection::near()
method. Alternatively, you can debug the entire query array generated by the builder by using the Query::getQuery()
method.
来源:https://stackoverflow.com/questions/14123627/wrong-distance-in-geonear-method-with-doctrine-mongodb-odm