Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda