3

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 1

4

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]);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to do it inside single function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.