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

197
Vistas
how to use getter or setter with mongoose timestamps?

I want to use a getter or setter for timestamps fields in the schema (mongoose schema) in order to change those dates formats when fetching them from the database.

let schema = new mongoose.Schema(
     {
        name: String,
        dob: {
                type: Date,
                get: (date) => {
                  if (date) return date.toISOString().split("T") [0];
                },
          },
     },
     {
          timestamps: true,
     }
);

is there a clear solution for this ? Thanks in advance.

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

0

i found this solution works well.


let schema = new mongoose.Schema(
     {
        name: String,
        dob: {
                type: Date,
                get: (date) => {
                  if (date) return date.toISOString().split("T") [0];
                },
        },

        createdAt: {
                type: Date,
                get: (date) => timeSince(date),
        }
        updatedAt: {
                type: Date,
                get: (date) => timeSince(date),
        },
     },
     {
       timestamps: true,
       toJSON: { getters: true, virtuals: true },
     }
);

where timeSince is a function i defined. and i use the json version of the model when i fetch it.

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