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:
JavaScript
x
4
1
def write_toDB(file_name, my_key):
2
3
db.file_name.insert_one(data)
4
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:
JavaScript
1
4
1
def write_toDB(file_name, my_key):
2
3
db[file_name].insert_one(data)
4