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

130
Vistas
How to automatically add timestamps eg. createdAt/updatedAt in MongoDB nodejs driver

I'm working on a small application and I need to automatically add a timestamp of creating/updating my documents in the default Nodejs MongoDb driver. Right now, when I add a new document, it only has added the _id field. Is there a way to force the addition of the createdAt/updatedAt fields?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can add createdAt manually when creating the object this way:

        await this.collection.insertOne({ businessAuth: _id, url, labels, createdAt: new Date()});

And updatedAT when updating the object this way:

video.updatedAt = new Date();
const saved = await this.collection.findOneAndUpdate({_id: video._id}, {$set: {...video}});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Drivers do not generally provide this functionality. ODMs do. In case of Node, mongoose is an ODM which does.

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