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

250
Vistas
Convert mongoose createdBy timestamp into Unix timestamp

I am a bit new to MongoDB and Mongoose and I want to convert the created timestamp from Mongo into a Unix timestamp for easier comparison.

    async getComments(params) {
    //user who the comments was made for
    let _id = params;
    try {
        //get comments for current user
        let uFilter = { user: _id };
        let uFields = { comment: 1, createdBy: 1, created: 1 };
        const resultsData = await Comments.find(uFilter, uFields).populate({
            path: 'createdBy',
            select: ['firstName', 'lastName'],
        });

        console.log(resultsData);
        return { d: resultsData };
    } catch (error) {
        console.log(error);
        return false;
    }
}

What I get back is:

 {
_id: new ObjectId("0000000000000000000"),
comment: 'Comment made by user',
created: 2022-01-13T09:21:25.689Z,
createdBy: {
  _id: new ObjectId("61b750324596e539c7cadb9b"),
  firstName: 'John',
  lastName: 'Doe'
}

}

so the created field I want to convert into a Unix timestamp. Any suggestions?

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

0

This is how I do it in the frontend:-

const { DateTime } = require('luxon')

module.exports.getMongoTimestamp = timestamp => {
    const timeStampArgs = timestamp.toString().split(' ')
    const timestampUnix = DateTime.fromRFC2822(`${timeStampArgs[0]}, ${timeStampArgs[2]} ${timeStampArgs[1]} ${timeStampArgs[3]} ${timeStampArgs[4]} ${timeStampArgs[5].replace('GMT', '')}`).toMillis()
    return parseInt(timestampUnix, 10)
}

I am using luxon to do 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