【问题标题】:What is the correct query for a Doctrine ODM Geospatial query?Doctrine ODM 地理空间查询的正确查询是什么?
【发布时间】:2012-01-17 22:01:36
【问题描述】:

我已经设置了一个类,如
http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/geospatial-queries.html

所述

我的二维索引在 mongo 中设置正确

> db.system.indexes.find()                                          
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "pfcd_dev.media", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "pfcd_dev.events", "name" : "_id_" }
{ "v" : 1, "key" : { "coordinates" : "2d" }, "ns" : "pfcd_dev.events", "name" : "coordinates_" }

当我在 mongo shell 上运行以下命令时,会返回大量结果。

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

我已经为near() 尝试了以下所有变体,都返回零结果

$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();

Doctrine 网站上的文档似乎有点不对劲。仅作记录,我创建了一个显示我的课程的要点(经过整理)。 https://gist.github.com/1627491

这是debug()的输出

$this->dm->createQueryBuilder('\Application\Event')->field('coordinates')->near('50,60')->debug();

    Array
    (
        [type] => 10
        [mapReduce] => Array
            (
                [map] => 
                [reduce] => 
                [options] => Array
                    (
                    )

            )

        [near] => Array
            (
                [coordinates] => 50,60
            )

    )

【问题讨论】:

    标签: mongodb doctrine geospatial


    【解决方案1】:

    您是否尝试过为您的模型删除 extends Base

    您也可以尝试以下方法:

    $places = $this->dm->createQueryBuilder('\Application\Event')->field('coordinates.latitude')->near(50)->field('coordinates.longitude')->near(60)->getQuery()->execute();
    

    【讨论】:

    • 我还没有尝试删除 extends Base,但我很好奇为什么这会影响查询的结果。 Base 是一个抽象类,我只是为了处理所有模型共有的一些简单操作。我能够成功地针对 ODM 运行其他查询。
    • 你已经为自己赢得了 +50 点数,我很乐意提供。您提供的解决方案完美运行。休息一天。
    • 为了更清楚,查询效果很好,但我没有摆脱或extends Base
    • 谢谢。我在想如果 Base 类有其他属性,驱动程序可能不知道哪些属性与 2d 索引相关。然后我找到了一种方法来指定你要使用哪些属性,这显然是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2013-09-25
    • 2018-04-06
    相关资源
    最近更新 更多