I need to run a query which returns total count of documents together with documents and that can be limited and offset. It's similar to this and this question. The difference is that I run map/reduce and total count is already available in stats argument so hopefully I don't have to call the query twice.
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);
});
});
I call the list function from controller like this:
Track.list(options, function (err, docs) {
res.json(docs);
});
Is it somehow possible to pass stats.counts.output to the controller together with returned documents?
You could wrap the cb within a function in the argument to exec:
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})
})