4

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:

query = {"$and": [
                     {"id_node": id_node}
                     {"port": port},
                     {"datetime": {"$gte": self.hourBegin}}
                     {"datetime": {"$lte": self.now}}
                     ]}
listResults = db.mycollection.find (query)

I also tried this way:

query = {"id_node": int(id_node)
                     "port": port,
                     "datetime": {"$gte": self.hourBegin, "$lte": self.now}}
listResults = db.mycollection.find (query)

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.

A list of possible results:

{ "_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" }
7
  • remove the space in your query operator. Use $gte instead of $ gte. Same for $lte. Commented Oct 14, 2014 at 3:57
  • also use the second variant of the query, but your query object is not valid. The id_no field is missing a :. Commented Oct 14, 2014 at 3:59
  • Can you update your question to include a sample doc you expect to be included in the results? Commented Oct 14, 2014 at 4:12
  • Ok, i did, add the sample doc. Commented Oct 14, 2014 at 4:27
  • What is type of self.hourBegin and self.now? how do they look? Commented Oct 14, 2014 at 6:19

1 Answer 1

5

I visit http://www.querymongo.com/, and then created a query as if it were in MySQL, then had to return one mongodb query. like this:

#MySQL Query
SELECT * FROM inoshare WHERE id_nome = 1 AND port = 1 AND datetime > 'hourBegin' AND datetime <= 'NOW()';

And the return:

db.inoshare.find({
"id_nome": 1,
"port": 1,
"datetime": {
    "$gt": "hourBegin",
    "$lte": "NOW()"
}
});

Then replace 'hourBegin' and 'NOW ()' for variables. Thank's for all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.