title: Query by time range in mongodb using pymongo
body: <p>Need to perform a query on a database mongodb resulting in a set of results by time range. I'm using pymongo. My query is as follows:</p>
  
  <pre><code>query = {"$and": [
                       {"id_node": id_node}
                       {"port": port},
                       {"datetime": {"$gte": self.hourBegin}}
                       {"datetime": {"$lte": self.now}}
                       ]}
  listResults = db.mycollection.find (query)
  </code></pre>
  
  <p>I also tried this way:</p>
  
  <pre><code>query = {"id_node": int(id_node)
                       "port": port,
                       "datetime": {"$gte": self.hourBegin, "$lte": self.now}}
  listResults = db.mycollection.find (query)
  </code></pre>
  
  <p>But the result is always empty. Tested directly in mongodb but the result is empty. I'm sure the data exists in the database with a time range that I'm researching.</p>
  
  <p>A list of possible results:</p>
  
  <pre><code>{ "_id" : ObjectId("543618c6e7b9914c35266128"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:04Z"), "valor" : "22.00", "sensor" : "2" }
  { "_id" : ObjectId("543618c6e7b9914c35266129"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:04Z"), "valor" : "0", "sensor" : "1" }
  { "_id" : ObjectId("543618c6e7b9914c3526612c"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:06Z"), "valor" : "22.00", "sensor" : "2" }
  { "_id" : ObjectId("543618c6e7b9914c3526612d"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:06Z"), "valor" : "0", "sensor" : "1" }
  { "_id" : ObjectId("543618c7e7b9914c35266130"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:08Z"), "valor" : "22.00", "sensor" : "2" }
  { "_id" : ObjectId("543618c7e7b9914c35266131"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:08Z"), "valor" : "0", "sensor" : "1" }
  { "_id" : ObjectId("543618c7e7b9914c35266134"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:10Z"), "valor" : "22.00", "sensor" : "2" }
  { "_id" : ObjectId("543618c7e7b9914c35266135"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:10Z"), "valor" : "0", "sensor" : "1" }
  { "_id" : ObjectId("543618c7e7b9914c35266138"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:12Z"), "valor" : "22.00", "sensor" : "2" }
  { "_id" : ObjectId("543618c7e7b9914c35266139"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:12Z"), "valor" : "0", "sensor" : "1" }
  </code></pre>