Nodejs multiple Queries Mongodb/Mongoose



We store the vast majority of the information we have in MongoDB in light of the fact that it's so natural to store and oversee in a manner that permits us to continue changing the plan, which is supercritical and basic for new businesses like us who are continually testing by evolving things.
Most times, we need to get data from different collections for some users such as admin user.
In this tutorial, I want to displace some information for the admin. Admin wants to know:
- how many investors are currently on the platform?
- How many payout requests are on the desk?
- How many transactions have been done on the platform?
-  How many accounts have been activated?
- How many users are in level-1, level-2,level-3,level-4?

All the above pieces of information should be displayed to the admin user.

I will be using 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 an open API with middleware to verify authentication/authorization, next to the admin dashboard controller which will return some data to the user.

 The admin-controller.js file is where all the responses will take place.  The first thing to do is to define our models using mongoose instance.

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 are some constant value to get records from different collections.
const adminDashboard = (requestresponsenext)=> {
  //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"});


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

 Promise.all([findAllInvestorfindAllPayout,
     findAllTransact,findAllActivatecountAllLevel1countAllLevel2,
      countAllLevel3countAllLevel4]).then((values)=> {

Attack callback to the resolution and/or rejection of the promise.

 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


    //  admin dashboard route....
const adminDashboard = (reqresnext)=> {
  //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([findAllInvestorfindAllPayout,
     findAllTransact,findAllActivatecountAllLevel1countAllLevel2,
      countAllLevel3countAllLevel4]).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,Level2Level3Level4,
      findAllPayoutallTransactionActivatedUsers});
 });

}

The above response from the server can be interpreted as follows.


Post a Comment

0 Comments