0

I've been working with python and SQL for couple of weeks and the work well together. But, I had to try using mongoDB for data storage. I had a program with all my SQL queries perfectly running but, in mongoDB I can't find similar queries to do same things. For example, to get all the rows for a period of 24hours I put an SQL query like

    SELECT * FROM `logs` WHERE `etimestamp` BETWEEN "2017-05-30T00:00:00Z" AND 
    "2017-05-30T23:59:59Z" AND `machineid` = "XYZ" 

Now, that is simple enough. But, I searched for a similar query for mongoDB and I couldn't find any. I have data for 3 to 4 days and I want the data for each day to be stored in a different variable.

This is my test.py

import datetime
import pymongo
from datetime import datetime, timedelta

session = driver.session()
conn = pymongo.MongoClient()
db = conn.pymongo_test #pymongo_test is my database
col = db.logs #logs is my collection
a = col.find({"machineid":"XYZ"}) #all the data for that particular machine
print(a[0])

This is the structure of my document

{
    "_id" : ObjectId("593555fb1d41c83378a06dfc"),
    "machineid" : "XYZ",
    "data" : 0.366022,
    "etimestamp" : ISODate("2017-05-30T00:00:00.000Z"),
    "id" : 1443279,
    "edate" : ISODate("2017-05-30T00:00:00.000Z")
}

I want that query to run in my python code. It gives me this error when I update my query

Traceback (most recent call last):
File "test.py", line 13, in <module>
a = col.find({"machineid":"XYZ","etimestamp":ISODate("2017-05-
30T00:00:00Z")})
NameError: name 'ISODate' is not defined
Jayesh Rohira
  • 77
  • 2
  • 8
  • I did run the query that was given in the similar question above and it runs perfectly in mongodb but I want to execute the query via python. Can someone help? please! – Jayesh Rohira Jun 06 '17 at 14:57
  • https://stackoverflow.com/a/43731228/7709276 this is what I was looking for – Jayesh Rohira Jun 07 '17 at 07:47

0 Answers0