2014-11-11 5 views
3

아래는이를 위해 사용되는 어떤 거의 Average Aggregation Queries in MeteorMongoDB를 집계

에서 사본 내 게시 기능입니다 다음 문서 (유성 응용 프로그램)를 추가합니다. 그런 다음 사용자는 하나의 항목 (레스토랑)을 클릭하고 세부 정보를 볼 수 있습니다. 세부 내에서 목록을 통해 cicle 수 있도록 앞뒤로 화살표가 있습니다.

내 생각은 그 집계 방법을 사용하여 이전 및 다음 문서 ID로 모든 문서를 풍부하게하여 이전 및 다음 링크를 쉽게 구축 할 수있게하는 것이 었습니다.

하지만 어떻게? 나는 모른다. 어쩌면 더 좋은 방법이 있을까요?

Meteor.publish("restaurants", function(coords, options) { 
    if (coords == undefined) { 
     return false; 
    } 
    if (options == undefined) { 
     options = {}; 
    } 
    if (options.num == undefined) { 
     options.num = 4 
    } 
    console.log(options) 
    /** 
    * the following code is from https://stackoverflow.com/questions/18520567/average-aggregation-queries-in-meteor 
    * Awesome piece!!! 
    * 
    * */ 
    var self = this; 
    // This works for Meteor 1.0 
    var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db; 

    // Your arguments to Mongo's aggregation. Make these however you want. 
    var pipeline = [ 
     { 
     $geoNear: { 
      near: { type: "Point", coordinates: [ coords.lng , coords.lat ] }, 
      distanceField: "dist.calculated", 
      includeLocs: "dist.location", 
      num: options.num, 
      spherical: true 
     } 
     } 
    ]; 

    db.collection("restaurants").aggregate(  
     pipeline, 
     // Need to wrap the callback so it gets called in a Fiber. 
     Meteor.bindEnvironment(
      function(err, result) { 
       // Add each of the results to the subscription. 
       _.each(result, function(e) { 
        e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10; 
        self.added("restaurants", e._id, e); 
       }); 
       self.ready(); 
      }, 
      function(error) { 
       Meteor._debug("Error doing aggregation: " + error); 
      } 
     ) 
    ); 
}); 

답변

0

결과에 대한 문서가 많지 않은 경우 JavaScript로 처리 할 수 ​​있습니다. 가입 코드를 이와 같이 변경하십시오 (이 코드를 테스트하지 않았습니다).

_.each(result, function(e, idx) { 
    if(result[idx - 1]) e.prev = result[idx - 1]._id; 
    if(result[idx + 1]) e.next = result[idx + 1]._id; 
    e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10; 
    self.added("restaurants", e._id, e); 
});