title: Substituting Variables Value in Mongodb statement
body: <p>My main intention is to dynamically change the Employees collection while using pymongo, and i was able to do it for insert commands, I am facing problems with the find command, no matter what i do exec() always returns None. but if i copy the string and run it value gets assigned to the variable.</p>
  
  <p>can someone throw some light on why the exec is unable to return a resultset or assign a the resultset to a variable?</p>
  
  <pre><code>db.Employees.update_one(
          {"id": criteria},
          {
          "$set": {
              "name":name,
              "age":age,
              "country":country
          }
          }
      )
  
  from pymongo import MongoClient
  import ast
  
  client = MongoClient('localhost:27017')
  db = client.TextClassifier
  </code></pre>
  
  <p>insert works</p>
  
  <pre><code>def mongo_insert_one(COLLECTION_NAME, JSON):
      QUERY = """db.%(COLLECTION_NAME)s.insert_one( %(JSON)s )""" % locals();
      exec(QUERY)    
  
  def mongo_retrive(COLLECTION_NAME, JSON):
      resultset = None
      query = """resultset = db.%(COLLECTION_NAME)s.find( %(JSON)s )""" % locals();
      return resultset
  
  
  print(mongo_retrive('hungry_intent', "{'Intent':'Hungry'}"))
  </code></pre>
  
  <p>neither this would work</p>
  
  <pre><code>resultset = exec(""" db.%(COLLECTION_NAME)s.find( %(JSON)s )""" % locals();)
  </code></pre>
  
  <p>this would not work for an entirely different reason,it says If you meant to call the 'locals' method on a 'Database' object it is failing because no such method exists.</p>
  
  <pre><code>resultset = db.locals()[COLLECTION_NAME].find()
  </code></pre>