【问题标题】:PyMongo inserting takes too much timePyMongo 插入需要太多时间
【发布时间】:2020-06-15 08:16:57
【问题描述】:

我有一些“.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】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多