Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Return variable from database find instead of array

I need to pull two values out of my mongoDB database and right now the code is returning an array. How do I get:

  1. totalGuests
  2. attendedGuests

out of the query and stored in variables so I can display them on the client?

module.exports.showEvent = async(req, res,) => {
    const event = await Event.findById(req.params.id).populate('artist');
    if (!event) {
        req.flash('error', 'Cannot find that Event');
        return res.redirect('/events');
    }
    res.render('events/show', { event });
    const { guest_id } = req.cookies;
    const lookUp = Event.collection.find({ _id: req.params.id},
      {
        _id: 1,
        name: 1,
        guests: 1,
        totalGuests: {
          $size: "$guests"
        },
        attendedGuests: {
          $size: {
            "$filter": {
              input: "$guests",
              cond: {
                $eq: [
                  "$$this.attended",
                  "Y"
                ]
              }
            }
          }
        }
      });
    console.log(lookUp);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!