2011-05-02 2 views
14

Node.js와 Mongoose를 사용하여 MongoDB에 액세스하고 있습니다. 일부 지리적 좌표를 저장하는 모델을 사용하고 있습니다. 색인이 생성되고 모든 것이 예상대로 작동하는 것 같습니다. 내가하려는 것은 나의 요청에 가장 가까운 것들을 검색하는 것이다.몽구스로 runCommand를 실행하는 방법은 무엇입니까?

distances = db.runCommand({ geoNear : "deals", near : [11.252, 14.141], spherical : true, maxDistance : 300 }).results 

그러나, 나는 몽구스와 함께이 작업을 수행하는 방법을 잘 모르겠습니다 다음 MongoDB를 콘솔에서이 같은 일을한다. http://www.mongodb.org/display/DOCS/Geospatial+Indexing

감사합니다, 호세

답변

0

내가 몽구스를 생각하지 않는 지금 runCommand 지원 : 여기 내가 사용하려고 명령에 대한 자세한 정보입니다. find 메소드를 사용하여 지형 공간 옵션을 사용하는 것이 더 좋습니다. >

db.places.find({ location : { $near : [50,50] , $maxDistance : 300 } }) 
+1

그러나 이것은 나에게 내가하고 싶은 거리를 반환하지 않습니다! 어떤 아이디어? – jsimoes

+0

코드를 보여줄 수 있습니까? – neebz

1
YourModel.db.db.executeDbCommand({geoNear : "locations", near : [11.252,14.141], spherical: true }, function(err,res) { console.log(res.documents[0].results)}); 

노드 0.6.6, [email protected], MongoDB의 버전 v2.0.2

그 약간 해키 및 변경할 수있다.

+0

3.6x에서는 더 이상 작동하지 않습니다. API 문서를 살펴보면 비슷한 것은 없습니다. 이것을 이루기위한 다른 방법을 알고 있습니까? – occasl

+1

YourModel.db.db.command ({geoNear : "locations", 근처 : [11.252,14.141], spherical : true}, function (err, res) {console.log (res.documents [0] .results)}); – Carasel

5

먼저 계산 된 거리를 읽으려는 경우 mongoose와 함께 geoNear를 직접 사용하는 편의 용 래퍼가 없습니다.

그러나 당신은 몽구스에서 에러 처리가 조금 달랐다 내 연구 결과에서 예상의 편의를 조금 포기해야하지만 기본 MongoDB native driver에서 몽구스 컬렉션 proxies all collection methods 방금 ​​자신의 geoNear method를 사용할 수 있기 때문에

.

어쨌든, 이것은 당신이 사용할 수있는 방법을 말했다 API :

YourModel.collection.geoNear(lon, lat, {spherical: true, maxDistance: d}, function(err, docs) { 
    if (docs.results.length == 1) { 
    var distance = docs.results[0].dis; 
    var match = docs.results[0].obj; 
    } 
}); 

올바른 오류 처리 및 how to calculate the distances의 문서를 참조하십시오.

0

희망이 있습니다. Referrence의 URL : http://mongoosejs.com/docs/api.html

// 기존 점

Model.geoNear([1,3], { maxDistance : 5, spherical : true }, function(err, results, stats) { 
    console.log(results); 
}); 

// geoJson

var point = { type : "Point", coordinates : [9,9] }; 
Model.geoNear(point, { maxDistance : 5, spherical : true }, function(err, results, stats) { 
    console.log(results); 
}); 
관련 문제