将数据从Python插入MongoDB Atlas(从一个函数插入到另一个函数)-如何插入?

将数据从Python插入MongoDB Atlas(从一个函数插入到另一个函数)-如何插入?,python,mongodb,oop,web-scraping,crud,Python,Mongodb,Oop,Web Scraping,Crud,我从网站上收集了一些数据,我想将其插入MongoDB。需要关于如何以及在何处插入数据的帮助吗?另外,我想在DB中插入数据并动态创建一个集合 import pymongo from pymongo import MongoClient import pandas as pd import re def print_hi(name: object) -> object: # Use a breakpoint in the code line below to debug your scri

我从网站上收集了一些数据,我想将其插入MongoDB。需要关于如何以及在何处插入数据的帮助吗?另外,我想在DB中插入数据并动态创建一个集合

import pymongo
from pymongo import MongoClient
import pandas as pd
import re


def print_hi(name: object) -> object:
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.
建造师

class ConfirmedCases:
def __init__(self, id, county, province_state, country_region, lat, lon, date, value):
    self.id = id
    self.county = county
    self.province_state = province_state
    self.country_region = country_region
    self.lat = lat
    self.lon = lon
    self.date = date
    self.value = value
在新列中每天更新日期后迭代行的步骤

def collection():
url_confirmed = "link1 specified separately"
confirmed_df = pd.read_csv(url_confirmed)
confirmed_cases = []
pattern = "10/.*/.*"
re.compile(pattern)
tester = re.compile(pattern)
dates = []
for col in confirmed_df.columns:
    if tester.match(str(col)) is not None:
        dates.append(str(col))

for index, row in confirmed_df.iterrows():
    county = row['Admin2']
    state = row['Province_State']
    region = row['Country_Region']
    latitude = row['Lat']
    longit = row['Long_']
    id = str(county) + str(state) + str(region)
    for date in dates:
        value = row[date]
        confirmed = ConfirmedCases(id, county, state, region, latitude, longit, date, value)
        confirmed_cases.append(confirmed)
return confirmed
连接到mongoDB的函数

def connection_mongo():
client = MongoClient('link to atlas')
print(client.list_database_names())
db = client.mydata 
coll = db["cc"]
try:
    print("MongoDB version is %s" %
          client.server_info()['version'])
except 
   pymongo.errors.OperationFailure as error:

    print(error)

    quit(1)


collection()
connection_mongo()

大家好,我是从约翰·霍普金斯大学每天更新的关于新冠病毒19例的CSV文件中删除的。它每天在一个新的列中添加一些确诊病例,我可以迭代并正确地将其转换为行。现在,我想将这些数据插入MongoDB atlas。您能帮助我并让我知道如何在MongoDB中插入这些数据吗?我应该在哪里做?在新函数中还是在同一函数中?因为我在一个新函数中建立了连接,而我的数据处理正在其他函数中进行。Link:'