Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda