Answer the question
In order to leave comments, you need to log in
NodeJS MongoDB search between dates. How? Where?
Hello!
The bottom line is this: I send a query (query) to NodeJS with AngularJS SPA, parse it and send it to MongoDB for search. I can't search between certain dates in any way. He climbed everything, tried everything. Tell me what's wrong.
this is how I accept it on the server side (NodeJS):
if(req.query.period_start) {
start_period = new Date().toISOString(req.query.period_start);
search.period_start = {$gte: start_period};
}
if(req.query.period_end) {
end_period = new Date().toISOString(req.query.period_end);
search.period_end = {$lte: end_period};
}
var query = db.itemModel.find(search);
Answer the question
In order to leave comments, you need to log in
1) You always have the current date_time "new Date().toISOString()" in search.period_start and search.period_end, so it doesn't "search" for anything (" new Date().toISOString(req.query.period_start); " - doesn't work that way).
It's easier to send converted dates (.toISOString) from the client and substitute them as is on the server.
2) In the database, it is better to store dates in Date-format, and not in text.
The date is written in the database not only in ISOString, but also in UTC. That is, you must also take into account the time zone.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question