M
M
mpotemkin2014-12-20 12:27:54
MongoDB
mpotemkin, 2014-12-20 12:27:54

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);

Doesn't search anyway. I'm interested in applications between certain dates here. But, having checked directly in the MongoDB shell, I did not understand why he does not want to search there either. The date is stored in the ISODate in the database.
Tell me where to dig, where is the error?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2014-12-21
@lega

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.

K
Konstantin Kitmanov, 2014-12-20
@k12th

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 question

Ask a Question

731 491 924 answers to any question