I need to count the number of documents inside two collections (Devices, Rooms). I saved details inside Devices schema and Rooms Schema as separate collections. How to query both the collections and return the count of documents?.
1 Answer
You can try using count():
var devicesCountQuery = DevicesModel.count();
var roomsCountQuery = RoomsModel.count();
With mongo you have to do two single queries.
You can wrap it in a single call using Promise.all() (Mongoose supports promises):
Promise.all([
DevicesModel.count().exec(),
RoomsModel.count().exec()
]).then(function(counts) {
console.log('Devices count %d', counts[0]);
console.log('Rooms count %d', counts[1]);
});
1 Comment
Karthikeyan Prakash
Is it possible to do it inside single function?