R
R
Rag'n' Code Man2021-07-14 02:44:03
MongoDB
Rag'n' Code Man, 2021-07-14 02:44:03

In what time period should you look for people who are now X years old?

I have a little problem with the presentation of data in my head, so I'm writing here.
How to find all people who are now X years old? The age in the database is stored as the date of birth in ISO format.

I need to make a query to find all the people who are now some years old, let's say 16.
What is the lower date limit and what is the upper date limit?

Can January 1, 2005 be considered the lower limit, or can there be people who were born in 2004 but are still 16 years old?

Then the upper limit is December 31, 2005? or does it not cover all possible sixteen year olds? Or vice versa, will 17-year-olds also get into the issue?

Then it turns out something like this:

people.find({
    age: {$gte: new Date(new Date().getFullYear() - age, 0, 1), $lt: new Date(2005, 11, 31)}
})

The head refuses to work ... Maybe you should sleep.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hint000, 2021-07-14
@iDmitriyWinX

Lower limit: (current date - (X+1) years + 1 day)
Upper limit: (current date - X years)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question