I would like to get the number of objects in a Collection and then use that number to perform a find. The code currently looks like this:
function() {
TestModel.count({}, function(err, num) {
if (err) {
return callback(err, undefined);
}
options.skip = Math.max(0, Math.floor((num - limit) * Math.random()));
options.limit = limit;
TestModel.find(conditions, fields, options).exec(callback);
});
}
Where testModel
is a mongoose model. And this works great! However, I would like to also be able to return the entire query (including count
) pre-execution if callback
above is null so that users can append more options to the pipeline (e.g. populate). How can I accomplish this?