我有几个要插入到数据库中的“ .csv”文件,但是有很多数据,我的代码花费了太多时间。我想问一下,是否有其他方法可以插入数据?顺便说一句。我不想使用os.system(mongoimport ... )
,因为它会从我的电话栏中删除前面的0
。
这里是代码
def do_sync():
path = "share"
extension = ".csv"
results = glob.glob(f"{path}/*{extension}")
for result in results:
print(result)
csvfile = open(result, 'r')
reader = csv.DictReader( csvfile )
db=mongo_client.media_mongo
header= [ "No", "phone", "location"]
for each in reader:
row={}
for field in header:
row[field]=each[field]
db.main_hikanshou.insert(row)
尝试使用pymongo中的批量插入功能
您必须首先创建插入查询,然后一次性将所有查询发送到数据库。