title: PyMongo - Name must be an instance of Str
body: <p>I'm trying to read and write from a database on MongoDB Atlas and while I can read data from my collections just fine, any attempt to write to a collection causes PyMongo to raise an exception 'name must be an instance of str'. </p>
  
  <p>I'm guessing this is in reference to the MongoClient object but the thing is I am using a connection string. Can anyone help me with what I'm doing wrong?</p>
  
  <p>My code is as follows: (I've got a ton of comments to help me understand better, so please excuse the lack of brevity)</p>
  
  <pre><code>def setattributes(self, rowdict):
          """ a function to create a user. Assumes that only a data
          dict is provided. strips everything else and updates.
           what the data dict contains is your problem.
          """
          with UseDatabase(self.dbconfig) as db:
              collection = db.database[self.tablename]
              locationdict = {    #create a corresponding location entry
              'email' : rowdict['email'],
              'devstate' : 0,
              'location' : {
              'type': 'Point',
              'coordinates' : [ 0, 0 ]
              },
              'lastseen' : datetime.now()
              }
              try:
                  res = db.insertdata(collection, rowdict) #insert user data
              except Exception as e:
                  print("Error adding user to DB : %s" % e)
                  return False  # if you cant insert, return False
              try:  
                  loccollection = db.database[self.locationtable]
                  resloc = db.insertdata(loccollection, locationdict)
              except Exception as e: # if the status update failed
                  db.collection.remove({'email' : rowdict['email']}) 
                  #rollback the user insert - atomicity
                  return False
          return True
  </code></pre>
  
  <p>My Database code is as follows:</p>
  
  <pre><code>class ConnectionError(Exception):
      pass
  
  class CredentialsError(Exception):
      pass
  
  class UseDatabase:
      def __init__(self, config: dict):
          self.config = config
  
      def __enter__(self, config = atlas_conn_str):
          try:
              self.client = MongoClient(config)
              self.database = self.client['reviv']
              return self
  
          except:
              print("Check connection settings")
              raise ConnectionError
  
      def __exit__(self, exc_type, exc_value, exc_traceback):
          self.client.close()
  
      def insertdata(self, collection, data):
          post = data
          post_id = self.database[collection].insert_one(post).inserted_id
          return post_id
  
      def getdetails(self, collection, emailid):
          user = collection.find_one({'email' : emailid}, {'_id' : 0})
          return user
  </code></pre>