I am trying to code a Python function to insert my CSV file to MongoDB database using MongoClient() but I don’t know how to dynamically code the collection name. Here is my code:
def write_toDB(file_name, my_key): ... db.file_name.insert_one(data)
If I write my code like above, the collection name will be “file_name” instead of the parameter file_name. Anyone knows how to fix this? Thank you!
Advertisement
Answer
You can reference the collection using this format:
def write_toDB(file_name, my_key): ... db[file_name].insert_one(data)