Multiple queries in NodeJS and MongoDB

I’ve got a NodeJS/Express/MongoDB app. For one of my endpoints I’m trying to get some stats from the database and I’m having trouble doing it using promises.

I know that db doesn’t get moved between the different .thens but no matter how I re-arrange the code I can’t get anything out of the console.log() except the first users count. I’ve also tried saving db into a variable declared at the start of the function but that doesn’t help either.

What’s the proper way to make multiple queries to MongoDB in a promisified way?

Node Code:

function getStats(num){
    var stats = "";

        .then(db => {
                    .then( userCount => {
                        stats += `Users: ${userCount}\n`
                        return db;
                .then( adminCount => {
                    .then( adminCount => {
                        stats += `Admins: ${adminCount}\n`
                .then( data => {
                    console.log(`Stats are now: \n${stats}`);
        .catch(err => {
            if(err) console.log(err);



116 thoughts on “Multiple queries in NodeJS and MongoDB”

  1. Pingback: sildenafil 20mg
  2. Pingback: buy viagra
  3. Pingback: liquid cialis
  4. Pingback: get cialis online
  5. Pingback: viagra cheap
  6. Pingback: cialis orange pill
  7. Pingback: cialis logo
  8. Pingback: generic viagra
  9. Pingback: cheapest viagra
  10. Pingback: cialis pharmacy
  11. Pingback: 35 viagra
  12. Pingback: cialis price
  13. Pingback: tadalafil peptide
  14. Pingback: sildenafil online
  15. Pingback: viagra canada
  16. Pingback: female viagra
  17. Pingback: sildenafil
  18. Pingback: women take viagra
  19. Pingback: herbal viagra
  20. Pingback: viagra ohne rezept
  21. Pingback: viagra walmart
  22. Pingback: viagra amazon
  23. Pingback: sildenafil prices
  24. Pingback: viagra pill
  25. Pingback: cialis best price

Leave a Comment