PyMongo插入需要太多时间

问题描述 投票:0回答:1

我有几个要插入到数据库中的“ .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)
python python-3.x insert pymongo
1个回答
0
投票

尝试使用pymongo中的批量插入功能

您必须首先创建插入查询,然后一次性将所有查询发送到数据库。

© www.soinside.com 2019 - 2024. All rights reserved.