question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't compare Date object

See original GitHub issue
await this.webhooksModel.insertMany([
  {
    ...data,
    nextSchedule: new Date()
  }
])

await this.webhooksModels.find(
  {
    nextSchedule: { $lte: new Date() } // new Date(new Date().toISOString())
  }
)

// returns []

If the data type was (new Date).toISOString() it will return data with the following query

await this.webhooksModel.insertMany([
  {
    ...data,
    nextSchedule: (new Date()).toISOString()
  }
])

await this.webhooksModels.find(
  {
    nextSchedule: { $lte: new Date(new Date().toISOString()) }
  }
)

// returns [...data]

However, this will cause problems in the MongoDB itself. We are unable to sort the data if the data type was string instead of Date

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
rvdendecommented, Oct 21, 2020

@KirianCaumes thank you this was helpful, but I found your workaround only works down to number of day and not hours, seconds, and so on. So for more accurate finding of start/end timestamps use:

let from = new Date("2020-10-21T14:41:51.578Z");

let dbresult = await this.db.packets.aggregate([
        {
          $addFields: {
            date: {
              $dateToString: {
                format: "%Y-%m-%dT%H:%M:%S:%LZ",
                date: "$timestamp",
              },
            },
          },
        },
        {
          $match: {
            id: req.parsedBody.id,
            date: {
              $gte: from,
            },
          },
        },
      ]).catch((err) => {
        console.log(err);
      });
1reaction
KirianCaumescommented, Oct 21, 2020

A workaround that I use is:

mycollection.aggregate([
    {
        $addFields: {
            date: {
                $dateToString: {
                    format: "%Y-%m-%d",
                    date: "$date"
                }
            }
        }
    },
    { 
        $match: {
            date: {
                $gte: new Date(`2020-01-01T00:00:00.000Z`),
                $lt: new Date(`2021-01-01T00:00:00.000Z`)
            }
        } 
    },
])

So I can keep date in date format in my db 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - can't compare datetime.datetime to datetime.date
You can use the datetime.datetime.combine method to compare the date object to datetime object, then compare the converted object with the other datetime ......
Read more >
JavaScript Date Comparison – How to Compare Dates in JS
The date object allows us to perform comparisons using the > , < , = , or >= comparison operators, but not the...
Read more >
Everything You Should Know about Comparing Dates in ...
So, as an alternative way to compare dates with time in Javascript, we can use the date object's built-in method, getTime . The...
Read more >
You Can Compare Dates In Javascript | by Filip Vitas - Medium
I stumbled upon the blog post named You Cannot Compare Dates In Javascript. The title is a bit misleading and it doesn't give...
Read more >
How to compare dates with TypeScript or JavaScript
As the title for this section says, we will take a look at direct date comparison, i.e comparing the Date objects directly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found