欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pymongo insert_many 批量插入的實例

 更新時間:2020年12月05日 11:11:23   作者:luoganttcc  
這篇文章主要介紹了pymongo insert_many 批量插入的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

lt=client.fangjia.district_stat_all_0416
dl = dt.find(query)

bf=[]
for m in dl:
  bf.append(m)
  if len(bf)==20000:
    lt.insert_many(bf)
    bf=[]
lt.insert_many(bf)

補(bǔ)充知識:Python操作Mongodb插入數(shù)據(jù)的兩種方法:insert_one()與insert_many()

代碼說明:

將mysql中table的數(shù)據(jù)插入到mongodb里

sys.setdefaultencoding('utf8')
import web
from pymongo import MongoClient
class getPltfList(object):
  def __init__(self):
    self.db1 = web.database(dbn='mysql', db = 'episode', user = 'root', pw= 'abc111--', host = '127.0.0.1')
    self.db1.printing = False
    self.db2 = web.database(dbn='mysql', db = 'episode_soc', user = 'root', pw= 'abc111--', host = '127.0.0.1')
    self.db2.printing = False
    self.db3 = web.database(dbn='mysql', db = 'cl_episode', user = 'root', pw= 'abc111--', host = '127.0.0.1')
    self.db3.printing = False
    self.db4 = web.database(dbn='mysql', db = 'cl_episode_soc', user = 'root', pw= 'abc111--', host = '127.0.0.1')
    self.db4.printing = False
  def __call__(self):
    return self.createPltfList()

  def createPltfList(self):
    self.ckpltfList = list(self.db1.query('select name, ip from EPISODE_PLTF_INFO order by id DESC'))
    self.ckpltfList += list(self.db2.query('select name, ip from EPISODE_PLTF_INFO order by id DESC'))
    self.clpltfList = list(self.db3.query('select name, ip from EPISODE_PLTF_INFO order by id DESC'))
    self.clpltfList += list(self.db4.query('select name, ip from EPISODE_PLTF_INFO order by id DESC'))

    return self.ckpltfList,self.clpltfList

if __name__ == '__main__' :
  mpList = list()
  flag = 0
  tmp = 0
  sum1 = 0
  sum2 = 0
  pltfList = getPltfList()()
  client = MongoClient("127.0.0.1", 27017)
  mdb = client.episode
  collection = mdb.pltf_basic_info
  # print (type(pltfList[1])) #list
  result= list()
  for pltf_my in pltfList[0]:
    pltf_mog = collection.find()
    for record in pltf_mog:
      IP = record.get('Cfg').get('Debug_IP')
      Name = record.get('Cfg').get('Register_Name')
      # print IP, Name
      if IP == pltf_my['ip'] and Name == pltf_my['name'] :
        flag = 1
        # print IP, Name
        break
      else:
        flag = 0

    if flag == 0 :
      data1 = {"Cfg" : {"Debug_IP" : pltf_my['ip'],"Register_Name":pltf_my['name'], "Site":"SH-CK"} }
      # print data1
      result.append((data1))
      # collection.insert_one(data1)
      # collection.delete_one(data)
      sum1 = sum1+1
  # print len(result)
  # collection.insert_many(result)
  for pltf_my in pltfList[1]:
    pltf_mog = collection.find()
    for record in pltf_mog:
      IP = record.get('Cfg').get('Debug_IP')
      Name = record.get('Cfg').get('Register_Name')
      if pltf_my['ip'].encode("utf-8") == IP.encode("utf-8") and pltf_my['name'].encode("utf-8") == Name.encode("utf-8") :
        tmp = 1
        # print IP, Name
        break
      else:
        tmp = 0

    if tmp == 0 :
      data2 = {"Cfg" : {"Debug_IP":pltf_my['ip'],"Register_Name":pltf_my['name'], "Site":"SH-CL"} }
      # print data2
      result.append((data2))

      # collection.insert_one(data2)
      # collection.delete_one(data)
      sum2 = sum2+1

  collection.insert_many(result)
  print sum1,sum2

剛開始的時候我使用的是insert_one()方法,一條一條的插入到mongodb的集合里,但是計算出的sum有出入。

在調(diào)試的過程中我發(fā)現(xiàn):

注釋掉 # collection.insert_one(data1) # collection.insert_one(data2)

計算出來的sum1 = 193 sum2 = 222 這是合理的,因為ck_mysql里有193條記錄,cl_mysql里有234條記錄,mongod里有總共有15條,但是12條是與cl_mysql重復(fù),所以正確。

但是當(dāng)我去掉注釋使用 collection.insert_one(data1) collection.insert_one(data2)時,打印出的sum1=181 sum2 = 213

也就是說少了幾個數(shù)據(jù),我不知道去哪了。

想了好多辦法,于是采用insert_many()的方法插入。先定義一個list(),將每個數(shù)據(jù)(數(shù)據(jù)的類型是dict)追加到list里:reslult.append(data1/data2),最后result里就會含有所有的數(shù)據(jù),一起插入。

于是乎,問題解決了。

可是我還是很困惑insert_one()哪里出問題了!?。。。?!

以上這篇pymongo insert_many 批量插入的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python腳本化Git的操作詳解

    Python腳本化Git的操作詳解

    如何判定此次測試是否達(dá)標(biāo),代碼覆蓋率是衡量的標(biāo)準(zhǔn)之一,利用fastapi框架重寫了覆蓋率統(tǒng)計服務(wù),然后通過diff操作統(tǒng)計增量代碼覆蓋率,當(dāng)然要使用diff操作,避免不了與git打交道,那python如何操作gi t呢,本文將詳細(xì)介紹了Python腳本化Git的操作
    2024-03-03
  • PyInstaller安裝包瘦身指南

    PyInstaller安裝包瘦身指南

    直接pyinstaller打包的話,如果當(dāng)前pip環(huán)境安裝的庫較多的話,會導(dǎo)致生成的可執(zhí)行exe程序文件過大,本文詳細(xì)的介紹了PyInstaller安裝包瘦身的過程,感興趣的可以了解一下
    2023-03-03
  • 一文帶你掌握Python中enumerate函數(shù)和for循環(huán)的對比

    一文帶你掌握Python中enumerate函數(shù)和for循環(huán)的對比

    在Python編程中,循環(huán)是一項常見的任務(wù),而for循環(huán)是最常見的一種,然而,Python提供了enumerate函數(shù),它允許在迭代過程中訪問元素的同時獲得它們的索引,下面我們就來學(xué)習(xí)一下二者的區(qū)別吧
    2023-11-11
  • 在Python程序中操作MySQL的基本方法

    在Python程序中操作MySQL的基本方法

    這篇文章主要介紹了再Python程序中操作MySQL的基本方法,主要借助了MYSQLdb module,需要的朋友可以參考下
    2015-07-07
  • pytorch transform數(shù)據(jù)處理轉(zhuǎn)c++問題

    pytorch transform數(shù)據(jù)處理轉(zhuǎn)c++問題

    這篇文章主要介紹了pytorch transform數(shù)據(jù)處理轉(zhuǎn)c++問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • python+selenium實現(xiàn)登錄賬戶后自動點擊的示例

    python+selenium實現(xiàn)登錄賬戶后自動點擊的示例

    本篇文章主要介紹了python+selenium實現(xiàn)登錄賬戶后自動點擊的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 解決pip install psycopg2出錯問題

    解決pip install psycopg2出錯問題

    這篇文章主要介紹了解決pip install psycopg2出錯問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python?SQLAlchemy庫的實現(xiàn)示例

    Python?SQLAlchemy庫的實現(xiàn)示例

    SQLAlchemy庫是一個強(qiáng)大的工具,為開發(fā)人員提供了便捷的方式來處理與數(shù)據(jù)庫的交互,本文主要介紹了Python?SQLAlchemy庫的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 解讀Python中字典的key都可以是什么

    解讀Python中字典的key都可以是什么

    這篇文章主要介紹了解讀Python中字典的key都可以是什么,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Python全棧之學(xué)習(xí)JS(2)

    Python全棧之學(xué)習(xí)JS(2)

    這篇文章主要為大家介紹了Python全棧之JS,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評論