python - How to make a query date in mongodb using pymongo? -
i'm trying perform query date in mongodb, result empty. query follows:
//in begin code def __init__(self): self.now = datetime.now() self.db = conexaomongo() self.horainicio = self.now - timedelta(minutes=1) def resultadoconsulta(self, modo, porta, id_node): #print "porta e no ", porta, id_node resultadomongo = [] mediafinal = false try: json = {'id_no': int(id_node), 'datahora': {'$gte': self.horainicio, '$lt': self.now}, 'porta': porta} print "consulta ser realizada: ", json resultadomongo = self.db.querymongoone(json)
//variable resultamongo return empty.
obs: tried without using .isoformat() when put in mongodb directly, return results if add isodate. not return results:
db.inoshare.find( {'id_no': 1, 'datahora': {'$lte': '2014-09-24t07:52:04.945306', '$gte': '2014-09-24t07:51:04.958496'}, 'porta': 'a0'})
more if edit returns:
db.inoshare.find( {'id_no': 1, 'datahora': {'$lte': isodate('2014-09-24t07:52:04.945306'), '$gte': isodate('2014-09-24t07:51:04.958496')}, 'porta': 'a0'})
this sequence of records in database:
{ "_id" : objectid("5435be9ce7b9916e02ed2cb5"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:05z"), "valor" : "917", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cb9"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:07z"), "valor" : "932", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cbd"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:09z"), "valor" : "189", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cc1"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:11z"), "valor" : "853", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cc5"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:13z"), "valor" : "202", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cc9"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:15z"), "valor" : "398", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2ccd"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:17z"), "valor" : "947", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cd1"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:19z"), "valor" : "57", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cd5"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:21z"), "valor" : "395", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cd9"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:23z"), "valor" : "941", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2cdd"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:25z"), "valor" : "208", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2ce1"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:27z"), "valor" : "186", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2ce5"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:29z"), "valor" : "848", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2ce9"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:31z"), "valor" : "571", "sensor" : "1" } { "_id" : objectid("5435be9ce7b9916e02ed2ced"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:33z"), "valor" : "351", "sensor" : "1" } { "_id" : objectid("5435be9de7b9916e02ed2cf1"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:35z"), "valor" : "558", "sensor" : "1" } { "_id" : objectid("5435be9de7b9916e02ed2cf5"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:37z"), "valor" : "449", "sensor" : "1" } { "_id" : objectid("5435be9de7b9916e02ed2cf9"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:39z"), "valor" : "768", "sensor" : "1" } { "_id" : objectid("5435be9de7b9916e02ed2cfd"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:41z"), "valor" : "542", "sensor" : "1" } { "_id" : objectid("5435be9de7b9916e02ed2d01"), "lab" : "2", "porta" : "a0", "id_no" : 1, "datahora" : isodate("2014-09-24t07:51:43z"), "valor" : "763", "sensor" : "1" }
if print json variable of python, see like:
{'id_no': 1, 'datahora': {'$gte': datetime.datetime(2014, 9, 24, 8, 21, 38, 527653), '$lt': datetime.datetime(2014, 9, 24, 8, 22, 36, 677022)}, 'porta': 'a1'}
my mongndb class connection is:
#!/usr/bin/python pymongo import mongoclient class conexaomongo: def __init__(self): self.conditions = [] self.client = mongoclient("127.0.0.1", 27017, safe=true) self.db = self.client['inoshare'] self.ino = self.db.inoshare def querymongo(self, param1, param2=none, param3=none): try: if param2 != none: results = self.ino.find(param1, param2) elif param3 != none: results = self.ino.find(param1, param2, param3) else: results = self.ino.find(param1) #print "total de registros ", results.count() post in results: #print post self.conditions.append(post) return self.conditions except: print "erro ao executar query"
@joni correct, need use datetime
.
from datetime import datetime pymongo import connection # have updated , included complete code client = connection('localhost', 27017) db = client['database'] # database name inoshare = db['inoshare'] # convert date string 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 @ 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 can return results. perhaps data corrupt, or should post code review
Comments
Post a Comment