25/05/2015 Database / mongodb

In my last implementation using mongodb and nodejs, i need to return an object JSON for aplication with total records found.

var result = {
total: 1233323;
rows: [object,object,object,object]
}

My solution:

  var total = Publication.find().count(function(e, count){

    Publication.find(filter, function(err, data){
      var result = {
        totalNumberOfEntries : count,
        rows: data
      }
      callback(err, result);
    }).limit(pageSize).skip(pageNumber)
  });

 

I do two requests in mongodb.

The first request, return the total, and in the callback return a second request with total records found and total rows found.