0

I have a mongodb that has the dates stored as a string in the 'DDMMYYY' format. Is there an easy way to find all documents with a date that is older than a given date? The only thing I can think of doing is adding a new field in the document and write a script that takes the date string then converts it to a date object and saves it to the new field but I would rather avoid doing that. :)

Dev01
  • 13,292
  • 19
  • 70
  • 124
  • 1
    Time to get over putting that off. The core problem ( aside from storing as a string takes a lot more bytes than a BSON Date ) is that since your dates start with the "day" and not the "year", they are not "lexically ordered" even as a string. The only other alternative is to run a calculation over the whole collection every time you want to query. And that's not a real alternative. If the collection was small enough for calculating to not be a problem, then converting should equally be no issue. Converting the dates is the only real solution. – Neil Lunn Sep 20 '17 at 21:40
  • @NeilLunn Thanks. That's what I needed to hear although it's not what I wanted to hear. :) – Dev01 Sep 21 '17 at 00:53

0 Answers0