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

203
Vistas
How to properly get distinct values with Mongoose in large dataset with date filters with timezone?

I have a large MongoDB dataset of around 34gb and I am using Fastify and Mongoose for the API. I want to retrieve all list of unique userUuid from the date range. I tried the distinct method from Mongoose:

These are my filters:

    let filters = {
      applicationUuid: opts.applicationUuid,
      impressions: {
        $gte: opts.impressions
      },
      date: {
        $gte: moment(opts.startDate).tz('America/Chicago').format(),
        $lt: moment(opts.endDate).tz('America/Chicago').format()
      }
  }

This is my distinct Mongoose function:

return await Model.distinct("userUuid", filters)

This method will return an array with unique userUuid based from the filters.

This works fine for small dataset, but it has a memory cap of 16MB when it comes to huge dataset.

Therefore, I tried the aggregate method to achieve similar results, having read that it is better optimized. Nevertheless, the same filters object above does not work inside the match pipeline because aggregate does not accept string date that comes as the result of moment; but only JavaScript Date is accepted. However, JavaScript date dissregards all the timezones since it is unix based.

This is my aggregate function to get distinct values based on filters.

return await Model.aggregate(
        [
            {
                $match: filters
            },
            {
                $group: {
                    _id: {userUuid: "$userUuid" }
                    }
            }
        ]
    ).allowDiskUse(true);

As I said, $match does not work with moment, but only with new Date(opts.startDate), however, JavaScript's new Date disregards moment's timezone. Nor it has a proper native timezone. Any thought on how to achieve this array of unique ids based on filters with Mongoose?

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

0

This is the solution I came up with and it works pretty well regarding the performance. Use this solution for large dataset:

        let filters = {
            applicationUuid: opts.applicationUuid,
            impressions: { $gte: opts.impressions },
            $expr: {
                $and: [
                    {
                        $gte: [
                            '$date',
                            {
                                $dateFromString: {
                                    dateString: opts.startDate,
                                    timezone: 'America/Chicago',
                                },
                            },
                        ],
                    },
                    {
                        $lt: [
                            '$date',
                            {
                                $dateFromString: {
                                    dateString: opts.endDate,
                                    timezone: 'America/Chicago',
                                },
                            },
                        ],
                    },
                ],
            },
        }

        return Model.aggregate([
                { $match: filters },
                {
                    $group: {
                        _id: '$userUuid',
                    },
                },
                {
                    $project: {
                        _id: 0,
                        userUuid: '$_id',
                    },
                },
            ])
            .allowDiskUse(true)

Which will return a list of unique ids i.e.

[
  { userUuid: "someId" },
  { userUuid: "someId" }
]

Use the following method on small dataset which is more convenient:

let filters = {
    applicationUuid: opts.applicationUuid,
    impressions: {
      $gte: opts.impressions
    },
    date: {
      $gte: opts.startDate,
      $lte: opts.endDate
    }
  }

  return Model.distinct("userUuid", filters)

Which will return the following result:

[ "someId", "someOtherId" ]
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