2013-04-07 1 views
0

문서와 함께 문서의 총 개수를 반환하며 제한되고 상쇄 될 수있는 쿼리를 실행해야합니다. thisthis와 비슷합니다. 차이점은 map/reduce를 실행하고 총계가 이미 stats 인수에서 사용 가능하므로 잘하면 쿼리를 두 번 호출 할 필요가 없다는 것입니다. 반환 문서와 함께 컨트롤러에 stats.counts.output을 통과mapReduce의 몽구스 한도 및 총 개수

Track.list(options, function (err, docs) { 
    res.json(docs); 
}); 

은 어떻게 든 가능 :

list: function (options, cb) { 
    ... 
    this.mapReduce(o, function (err, model, stats) { 
     console.log('# of documents: %d ', stats.counts.output); 
     model.find() 
      .limit(criteria.perPage) 
      .skip(criteria.perPage * criteria.page) 
      .exec(cb); 
    }); 
}); 

는이 같은 컨트롤러에서 list 함수를 호출?

+0

감사 stats.counts.output''에 대한 참조. 하나의 쿼리에서'.count()'와'.find()'를하는 방법을 찾고 있었고 최소한'mapReduce'를 사용할 때는 가능했다. – hvrauhal

답변

0

당신은 exec에 인수 함수 내에서 cb을 포장 수 :

model.find() 
    .limit(criteria.perPage) 
    .skip(criteria.perPage * criteria.page) 
    .exec(function (err, docs) { 
     cb(err, docs, stats.counts.output) 
    }); 

Track.list(options, function (err, docs, count) { 
    res.json({docs: docs, count: count}) 
})