2011-08-30 65 views
0

我有symfony 2 + doctrineMongo,我試圖使用地理空間查詢 - >附近,但似乎這個查詢不起作用。總是返回一個空數組。DoctrineMongo near查詢

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

而且我有這個疑問在我的倉庫:

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

有錯誤嗎?我該如何解決它?

我的地方的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 } } 

這是我的索引:

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 }` 

當我使用蒙戈外殼不久的查詢我收到此錯誤:

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

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

謝謝您!

+0

當您運行在蒙戈外殼的'near'查詢會發生什麼會對你收集Geospartial指數?它會返回任何東西嗎? – DrColossos

+0

做這個查詢我得到這個錯誤:'db.Place.find({「coordinates」:{$ near:[50,50]}}) 錯誤:{ 「$ err」:「can not find special index :2d for:{coordinates:{$ near:[50.0,50.0]}}「, 」code「:13038 }' – David

+0

HenrikBjørnskov的答案似乎是解決方案。錯誤很清楚地說明了問題。 – DrColossos

回答

相關問題