0

I save a mongodb field 'date' as datetime object and use following queries

1.db.test_collection.find({'date': {'$gte': start_date_time}})
2. db.test_collection.find({"date": {"$gte": start_date_time, "$lte": end_date_time}

where start_date_time is (say)2014-06-03 00:00:00 and end_date_time 2014-06-03 23:59:59 i insert some values to mongodb, But I don't give any results. please help me to solve the issue.

4
  • Show your document. Your "date" field is possibly a string. Commented Jun 4, 2014 at 1:51
  • sample value in my db is datetime.datetime(2014, 6, 4, 3, 47, 25, 637000) Commented Jun 4, 2014 at 9:05
  • That isn't a document. You can actually edit your post to show what the document actually looks like. Commented Jun 4, 2014 at 9:07
  • @ Neil Lunn, thanks for your helping attitude. Actaully there was no error in my script> I used datetime.datetime.now() to save date to db, But apache time different from server time!. now I use datetime.datetime.utcnow(). The problem fixed Commented Jun 4, 2014 at 12:43

1 Answer 1

1

You should use the datetime type.

import datetime

db.test_collection.find({'date': {'$gte': datetime.datetime(2014, 6, 3, 16, 46) }})

You can see more in the documentation (http://api.mongodb.org/python/current/tutorial.html)

Hope it works fine for you!

Sign up to request clarification or add additional context in comments.

1 Comment

start_date_time am using is a datetime object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.