Nodejs one request multiple Queries Mongodb/Mongoose

Many a time we want to send a single HTTP request to the server and get every data needed

in one single call by chaining our data together. Most times we need to get data from different collections for some users such as Admin users.

In this tutorial, I will deliberate on the admin dashboard. Admin wants to know :

  • How many investor are currently on the platform.
  • How many transaction has been done on the platform.
  • How many payout request are on the desk.
  • How many account has been activated.
  • How many users are in specific levels (Level-1, Level-2, Level-3, Level-4)

The above data are in the different collections in the MongoDB database. We will get those data from different collections and chain them together. Let us see it in action.

I will be using nodejs and mongoose (ODM) Object Data modeling and MongoDB.
Certainly, we should API endpoint like the one below.

router.get('/admin-dashboard',jwt_helperVerify ,adminController.adminDashboard

The above is a secure API with middleware(jwt_helperVerify) to verify authentication and authorization. Next, the admin controller which returns some data as a response to the user.

The admin-controller.js file is where all the chaining and response will take place.  The first thing to do is to define our models using mongoose instances.

const mongoose = require('mongoose');

const User =  mongoose.model('User');

const Payout = mongoose.model('Payment');

const Transaction = mongoose.model('Transaction');

const AllPayout = mongoose.model('Payout');

below is some constant value to get records from different collections in the admin dashboard endpoint.

const adminDashboard = (request, response, next)=> {

//find all investor
const findAllInvestor = User.countDocuments({role:'INVESTOR'}); 

 // find all payouts
 const findAllPayout = AllPayout.countDocuments({});

//find all transaction 
 const findAllTransact = Transaction.countDocuments({});

 // find all activated accounts
 const findAllActivate = User.countDocuments({activate: true});

 // get count users level
 const countAllLevel1 = User.countDocuments({level:"LEVEL-1"});

 const countAllLevel2 = User.countDocuments({level:"LEVEL-2"});

const countAllLevel3 = User.countDocuments({level:"LEVEL-3"});

 const countAllLevel4 = User.countDocuments({level:"LEVEL-4"});

We will use the promise Constructor to Creates a Promise that is resolved with an array of results when all of the provided Promises are resolve or are rejected.

Promise.all([findAllInvestor, findAllPayout,
     findAllTransact,findAllActivate, countAllLevel1, countAllLevel2,
      countAllLevel3, countAllLevel4]).then((values)=> {

After the promise, get specific values according to their indexes.

     const investors = values[0];
     const findAllPayout = values[1];
     const allTransaction = values[2];
     const ActivatedUsers = values[3];
     const Level1 = values[4];
     const Level2 = values[5];
     const Level3 = values[6];
     const Level4 = values[7];

Next, we send a response with the following values. The complete code below


const adminDashboard = (req, res, next)=> {

  //find all investors
 const findAllInvestor = User.countDocuments({role:'INVESTOR'});  

 // find all payouts
 const findAllPayout = AllPayout.countDocuments({});

 //find all transaction 
 const findAllTransact = Transaction.countDocuments({});

  // find all activated accounts
 const findAllActivate = User.countDocuments({activate: true});

 // get count users level
 const countAllLevel1 = User.countDocuments({level:"LEVEL-1"});

 const countAllLevel2 = User.countDocuments({level:"LEVEL-2"});

 const countAllLevel3 = User.countDocuments({level:"LEVEL-3"})

 const countAllLevel4 = User.countDocuments({level:"LEVEL-4"});

 Promise.all([findAllInvestor, findAllPayout,
     findAllTransact,findAllActivate, countAllLevel1, countAllLevel2,
      countAllLevel3, countAllLevel4]).then((values)=> {

     const investors = values[0];
     const findAllPayout = values[1];
     const allTransaction = values[2];
     const ActivatedUsers = values[3];
     const Level1 = values[4];
     const Level2 = values[5];
     const Level3 = values[6];
     const Level4 = values[7];

     res.status(201).send({investors,Level1,Level2, Level3, Level4,
      findAllPayout, allTransaction, ActivatedUsers});
 });
}

At the client-side, you can get your value using a dot notation or response[‘object name’].

this.investors = res['investors'];
this.allTransaction = res['allTransaction'];
this.activatedUsers = res['ActivatedUsers'];
//etc

Below is a preview of the admin dashboard

3 thoughts on “Nodejs one request multiple Queries Mongodb/Mongoose

  1. I am really loving the theme/design of your site. Do you ever run into any browser compatibility issues? A handful of my blog audience have complained about my site not working correctly in Explorer but looks great in Chrome. Do you have any ideas to help fix this issue?

  2. I not to mention my buddies were found to be looking at the best secrets and techniques found on your website and before long got an awful suspicion I had not thanked the web site owner for those techniques. Most of the young boys are actually as a consequence very interested to see them and have in effect in truth been taking advantage of them. I appreciate you for actually being very accommodating as well as for using certain impressive subject matter millions of individuals are really wanting to be aware of. My sincere regret for not saying thanks to earlier.

Leave a Reply

Your email address will not be published. Required fields are marked *