I have a json
file with around 50 queries.
The json
file look like this:
{
"1": {
"mongodb":"mydb1.mongodbtime.find({\n \"timestamp1\": {\"$gte\": datetime.strptime(\"2010-01-01 00:05:00\", \"%Y-%m-%d %H:%M:%S\"),\n \"$lte\": datetime.strptime(\"2015-01-02 00:05:00\", \"%Y-%m-%d %H:%M:%S\")}},\n {\"id13\":1}),",
"mongodb1index":"mydb1.mongodbindextimestamp1.find({\n \"timestamp1\": {\"$gte\": datetime.strptime(\"2010-01-01 00:05:00\", \"%Y-%m-%d %H:%M:%S\"),\n \"$lte\": datetime.strptime(\"2015-01-02 00:05:00\", \"%Y-%m-%d %H:%M:%S\")}},\n {\"id13\":1}),"
},
"2": {
"mongodb":"mydb1.mongodbtime.find({\n \"timestamp1\": {\"$gte\": datetime.strptime(\"2010-01-01 00:05:00\", \"%Y-%m-%d %H:%M:%S\"),\n \"$lte\": datetime.strptime(\"2015-01-02 00:05:00\", \"%Y-%m-%d %H:%M:%S\")}},\n {\"id13\":1}),",
"mongodb1index":"mydb1.mongodbindextimestamp1.find({\n \"timestamp1\": {\"$gte\": datetime.strptime(\"2010-01-01 00:05:00\", \"%Y-%m-%d %H:%M:%S\"),\n \"$lte\": datetime.strptime(\"2015-01-02 00:05:00\", \"%Y-%m-%d %H:%M:%S\")}},\n {\"id13\":1}),
}
}
I have two collections
one named mongodbtime
and one called mongodbindextimestamp1
in the database mongodbtime
.
The code i have used in python for passing the query
and execute it look like this:
mydb1 = myclient["mongodbtime"]
with open("queriesdb.json",'r') as fp:
queries = json.load(fp)
db = {"mongodb": "mongodbtime", "mongodb1index": "mongodbtime"}
for num_query in queries.keys():
query = queries["1"]
print(query)
for db_name in db:
print(db_name)
run(query[db_name])
def run(query):
for j in range(0, 1):
start_time = time.time()
cursor = query
for x in cursor:
pprint(x)
# capture end time
end_time = time.time()
# calculate elapsed time
elapsed_time = end_time - start_time
times.append(elapsed_time)
#elapsed_time_milliSeconds = elapsed_time * 1000
#print("code elapsed time in milliseconds is ", elapsed_time_milliSeconds)
finalmeasurments(times)
I passed it like a string and obviously when i print(cursor)
it just print
the query
Should i use another form of file?
Any idea on how i should execute my query
?