0

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?

1 Answer 1

1

If you have a choice, refactor your approach. If you need to run a set of tests, create an Enum class with the parameters in and iterate the class.

If you must use a JSON file, consider eval() but YMMV.

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

4 Comments

what do you mean by ymmv?
generally eval type functions are considered bad practice and likely very hard to debug if things don't work.
i have one question more to ask.i use this code to benchmark postgres performance vs mongodb performance..do you think using eval add a lot execution time?
Sorry I've no idea.

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.