-1

I know this question has been asked earlier been none of the solutions worked for me.

I want to query a collection with createdAt of type DateTime. It seems to work perfectly in mongo shell but it returns empty array in Node.js. I am using following code:

{ 
 '$gte':new Date().toISOString()
}
m_callens
  • 6,100
  • 8
  • 32
  • 54

2 Answers2

1

You are querying a string instead of a date object. I would suggest using

new Date()

Instead of

new Date().toISOString()

and see if that works for you.

https://docs.mongodb.com/manual/core/shell-types/

Scott C
  • 21
  • 4
  • ISODate is not defined in `nodejs` . I have tried `new Date()` as well, it doesn't works too. – Ritish Gumber Jun 26 '17 at 16:14
  • Here is a much better explanation and possible solution for you. http://stackoverflow.com/questions/21286599/inserting-and-querying-date-with-mongodb-and-nodejs/21286896#21286896 – Scott C Jun 26 '17 at 16:49
0

You can use momentjs as :

moment.format()

or

moment.utc().format()

to create the timestamp. MomentJS will come really handy for other purposes as well.

sulav_lfc
  • 772
  • 2
  • 14
  • 34