2

I have a MongoDB collection where the date attribute is stored in the following format

{'date': "2014-05-28T13:02:46"}

I receive a query from the user in my Python server which is in the format '2014-05-28' that I have to compare against 'date'. I wrote the following query in Pymongo but it fails.

 signup_date = datetime.date(2014,5,28)
 signed_up_users = usercollection.find({'customer_id':'abc','date' :{"$gte":signup_date}})

I believe it's a date conversion issue.However, I can't modify the collection 'date' which is a string. So, I believe I have to parse it but can't find a way to parse it in PyMongo.

fsl
  • 3,250
  • 1
  • 10
  • 20
ubh
  • 607
  • 1
  • 10
  • 15
  • 1
    It's not a date conversion issue except that you should probably be using a date type to store dates. What is your expected result? Should `2014-05-28` come before or after 1:02PM on the same day? – wdberkeley Feb 17 '15 at 19:23
  • Unfortunately, I don't have control on how the data gets stored. I only read from it. Basically the date 2014-05-28 is used as a lower bound for comparison. – ubh Feb 19 '15 at 02:20

1 Answers1

1

I was able to resolve this in the following way

signup_date_iso = datetime.date(2014,5,28).isoformat()

This converts into an ISO format date string that I can use to compare with the date strings stored in the database.

ubh
  • 607
  • 1
  • 10
  • 15