R
R
Rag'n' Code Man2021-07-13 02:02:12
MongoDB
Rag'n' Code Man, 2021-07-13 02:02:12

How to filter the output of the database by age, if it is stored in the database as a date of birth, and you need to receive a specific number / numbers from the client?

In the database, the student's age is stored in the format MM-DD-YYYY, but then there was a need to filter students by age, moreover, as in Excel.

That is, we have a certain array of ages from the database, let's say we have students aged 13, 14, 15 years old, and these values ​​will be given to us as filter values, and we will mark them with a checkmark. That is, if we want students of only 14 and 15 years of age to get into the issue, then we must look in the database for those students who will meet this parameter.

What needs to be done or rewritten to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Romanov, 2021-07-13
@iDmitriyWinX

If you store in text, then it is better to convert to a date and make regular queries

let dStart = new Date();
let dEnd = new Date();
dStart.setFullYear(dStart.getFullYear() - 15);
dEnd.setFullYear(dEnd.getFullYear() - 13);
db.user.find({birthday: {$gte: dStart, $lt: dEnd}})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question