DoctrineMongo near query
I have symfony 2 + doctrineMongo and i'm trying to use the geospatial query ->near but seems that this query don't work. Always return an empty array.
I followed this guide: http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/geospatial-queries.html
And i have this query in my repository:
$this->createQueryBuilder()
->field('coordinates')->near($longitude, $latitude)
->getQuery()
->execute();
There is a bug? How i can fix it?
My places findAll:
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 } }
开发者_JAVA百科
And this is my index:
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 }`
When i use the near query in mongo shell i get this error:
db.Place.find( { coordinates : { $near : [50,50] } } )
error: {
"$err" : "can't find special index: 2d for: { coordinates: { $near: [ 50.0, 50.0 ] } }",
"code" : 13038
}
Thank you!
You need to have a Geospartial index on you collection like it is explained here http://www.mongodb.org/display/DOCS/Geospatial+Indexing
精彩评论