0

I have documents that have dates stored in MongoDB in the UTC format: 2016-01-28T01:00:00Z where Z represents the UTC time.

What I am trying to do is find all documents, for which the date has not expired (date_stored_in_DB > Today's Date). However I am confused about the right way of doing this.

Approach 1: Should I convert today's date into the Z-format (as shown above) first and start looking into the database? That way it would just be matching strings.

Approach 2: Should I get all records from the database and convert each date into the parsable date format like 02/03/2016 and compare with today's date? I am thinking regex here but I am not sure

Approach 3: Your suggestions?

I am using reactivemongo with scala & play framework to get this done.

cchantep
  • 9,118
  • 3
  • 30
  • 41
summerNight
  • 1,446
  • 3
  • 25
  • 52

2 Answers2

3

If you stored your date as an ISODate type then you can just query against it like so:

db.yourcollection.find({"date" : { $gte : new ISODate("2016-01-28T01:00:00Z") }});

The $gte operator queries for greater than or equal to. See this for additional informaton.

Querying all the records to then filter them out is typically not a good idea.

Al Iacovella
  • 446
  • 2
  • 5
1

The easiest way is to convert today's date to UTC:

 val utcString = new DateTime(now).withZone(DateTimeZone.UTC)

You can pass this value to a $gt MongoDB query to retrieve the non-expired records.

Alex
  • 21,273
  • 10
  • 61
  • 73