Time interval query in mongodb using pymongo

Asked

Viewed 853 times

3

I need to perform a query in a mongodb database that results in a set of results per time range. I am using pymongo. My consultation is as follows:

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

I tried that way too:

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

However 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.

1 answer

1


A lot will depend on what kind of data are datahora, self.horaInicioand self.now.

If datahora be saved in Mongodb as ISODate, which is the most common, so you can use datetime in self.horaInicioand self.now and any of your filters should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.