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

168
Views
Return value from mongodb (not array) using mongoose

I want to return a single value from my model so I can compare it to the the current time in my middleware. Everything I'm finding in the mongoose docs is to return an array... how do I just get my value for key 'event_start' out of below:

const eventSchema = new Schema({
  event_name: String,
  venue_name: String,
  address: String,
  event_start: {
    type: Date,
    required: [true, 'Date & time of event start required']
    },
  event_end: {
    type: Date,
    required: [true, 'Date & time of event end required']
  },
}

Here's what I have so far in my function. I'd like to find by id (can update below) since I have the event_id object stored in req.params:

module.exports.expiredEvent = (req, res, next) => { 
   const { id } = req.params;
   const event = Event.find({ "_id": id })

How do I return the value for event_start from my db instead of an object?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

module.exports.expiredEvent = async (req, res, next) => { 
   const { id } = req.params;
   const event = await Event.find({ "_id": id }, {_id: 0, event_start: 1});
   console.log(event); // [{event_start: 2022-01-13T17:26:00.000Z}]
   return event[0].event_start; // this will return 2022-01-13T17:26:00.000Z
}
about 4 years ago · Juan Pablo Isaza Report

0

Since you're using mongoose, you should try findById:

Event.findById(id, "event_start").exec();

Refer the document for more details.

about 4 years ago · Juan Pablo Isaza Report
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!