How to make a query date in mongodb using pymongo?

@Joni is correct, you need to use datetime.

from datetime import datetime
from pymongo import Connection

# i have updated and included the complete code 
client = Connection('localhost', 27017)
db = client['database'] # your database name
inoshare = db['inoshare']


# convert your date string to datetime object
start = datetime(2014, 9, 24, 7, 51, 04)
end = datetime(2014, 9, 24, 7, 52, 04)

inoshare.find( {'id_no': 1, 'datahora': {'$lt': end, '$gte': start}, 'porta': 'A0'})
<pymongo.cursor.Cursor at 0x7f9aafd64a90>

inoshare.find_one( {'id_no': 1, 'datahora': {'$lt': end, '$gte': start}, 'porta': 'A0'})

{u'_id': ObjectId('5435be9ce7b9916e02ed2cb5'),
 u'datahora': datetime.datetime(2014, 9, 24, 7, 51, 5),
 u'id_no': 1.0,
 u'lab': u'2',
 u'porta': u'A0',
 u'sensor': u'1',
 u'valor': u'917'}

clearly I can successfully return results.
Perhaps your data is corrupt, or you should post all your code for us to review

Leave a Comment

tech