2016-02-26 34 views
1

我想從mongodb中的nodejs文件中獲取數據(附近位置)。我在MongoDB中創建了一個索引,但是當我查詢它,我得到這個錯誤:我使用來從節點的數據

{ name: 'MongoError', 
message: 'Unable to execute query: error processing query: 
ns=event_management.locations limit=1000 skip=0\nTree: GEONEAR 
field=loc maxdist=500000 isNearSphere=0\nSort: {}\nProj: {}\n planned 
returned error: unable to find index for $geoNear query', 
'$err':Unable to execute query: error processing 
query:ns=event_management.locations limit=1000 skip=0\nTree: GEONEAR 
field=loc maxdist=500000 isNearSphere=0\nSort: {}\nProj: {}\n planner 
returned error: unable to find index for $geoNear query', 
code: 17007 } 

查詢:

domain.Location.find(
{ 
    loc:{ 
    $near :{$geometry:{type:"Point",coordinates: [parseFloat(longitude), parseFloat(latitude) ] }, 
    $maxDistance: parseFloat(distance)*1000} 
    } 
} 

並且使用命令創建索引是:

db.locations.ensureIndex({ loc: "2dsphere"}); 

我不明白我在哪裏錯了。如果有人願意,請幫助我。

在此先感謝。

+0

你是如何存儲緯度和經度在你DB? – Ashutosh

+0

LOC:{ 類型:{ 類型:字符串, 需要:真, 枚舉:[ '點', '線段形式', '多邊形'], 缺省: '點' }, 座標:[] } lat和long以字符串格式存儲。上面的代碼是在節點中定義的模型 –

回答

0

根據我的說法,有一個與距離有關的錯誤($ maxDistance)。你超出了限制。因此沒有找到具有該條件的索引。我對此很輕信。

domain.Location.find(
{ 
    loc:{ 
    $near :{$geometry:{type:"Point",coordinates: [parseFloat(longitude), parseFloat(latitude) ] }, 
    $maxDistance: parseFloat(distance)} 
    } 
} 

如果您發現它很有幫助,請立即投訴並接受它。

感謝&乾杯