Doctrine Mongo附近查询

时间:2011-08-30 18:25:49

标签: mongodb symfony

我有symfony 2 + doctrineMongo,我正在尝试使用地理空间查询 - >附近,但似乎此查询不起作用。总是返回一个空数组。

我遵循了这个指南:http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/geospatial-queries.html

我在我的存储库中有这个查询:

$this->createQueryBuilder()
            ->field('coordinates')->near($longitude, $latitude)
            ->getQuery()
            ->execute();

有错误吗?我怎么解决它?

我的地方找到了所有人:

db.Place.find();

{ "_id" : ObjectId("4e4b82df3eee4f7e2c000000"), "coordinates" : { "latitude" : 23.1, "longitude" : 23.23 }, "name" : "Opium Mar" }

{ "_id" : ObjectId("4e5769f43eee4fc002000000"), "name" : "Sutton club", "coordinates" : { "latitude" : 2, "longitude" : 1 } }

{ "_id" : ObjectId("4e5cf2173eee4fc202000008"), "name" : "Scorpia", "coordinates" : { "latitude" : 23, "longitude" : 22 } }

这是我的索引:

db.system.indexes.find();

{ "name" : "_id_", "ns" : "kzemos.User", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Place", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Party", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Friend", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.UserParty", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Invite", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Photo", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.Group", "key" : { "_id" : 1 }, "v" : 0 }

{ "name" : "_id_", "ns" : "kzemos.places", "key" : { "_id" : 1 }, "v" : 0 }

{ "_id" : ObjectId("4e5deaced3c5c27e84059447"), "ns" : "kzemos.places", "key" : { "loc" : "2d" }, "name" : "loc_", "bits" : 26 }

{ "_id" : ObjectId("4e5dead9d3c5c27e84059448"), "ns" : "kzemos.places", "key" : { "coordinates" : "2d" }, "name" : "coordinates_", "bits" : 26 }`

当我在mongo shell中使用near查询时出现此错误:

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

error: {
        "$err" : "can't find special index: 2d for: { coordinates: { $near: [ 50.0, 50.0 ] } }",
        "code" : 13038
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在集合中设置地理空间索引,如此处所述http://www.mongodb.org/display/DOCS/Geospatial+Indexing