Mankind1023 Mankind1023 - 1 month ago 18
Node.js Question

Mongoose - Search dates stored as strings by date range

I have a mongo db with a paymentDate field stored as strings YYYY-MM-DD (I can't change how these are unfortunately), and I need to do a date range search on them.

I found a post suggesting something like this:

coll.find({paymentDate: new RegExp("^(2016-02-18|2016-06-19)", "i")});


But I can't seem to get that to work at all.

Any help is appreciated, I've hit a dead end here.

Answer

Storing dates as strings in YYYY-MM-DD format works fine in this case as the string ordering matches the date ordering so you can do:

coll.find({paymentDate: {$gte: "2016-02-18", $lte: "2016-06-19"}});
Comments