Query by time interval in mongodb using pymongo

3

I need to perform a query on a mongodb database that results in one result set per time range. I'm using pymongo. My query looks like this:

queryConsulta = {"$and": [
                    {"id_no": id_node}, 
                    {"porta": porta},
                    {"datahora":{"$gte": self.horaInicio}},                      
                    {"datahora": {"$lte": self.now}}
                    ]}
listaResultados = db.minhacolecao.find(queryConsulta)

I've also tried it this way:

queryConsulta = {"id_no":int(id_node),
                    "porta":porta,
                    "datahora": {"$gte":self.horaInicio, "$lte":self.now }}
listaResultados = db.minhacolecao.find(queryConsulta)

But the result is always empty. I have tested directly on mongodb but the result is empty. I'm sure the data exists in the database with the time range I'm searching.

    
asked by anonymous 14.10.2014 / 04:31

1 answer

1

Much will depend on what data type are datahora , self.horaInicio and self.now .

If datahora is saved in MongoDB as ISODate , which is the most common, then you can use datetime in self.horaInicio and self.now and any of your filters should work.

    
17.11.2014 / 20:37