Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!