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

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

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 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