2012-03-20 42 views
7

我需要在Python代碼中從MongoDB中讀取整個集合(集合名稱是「test」)。我嘗試像如何讀取1000塊的集合?

self.__connection__ = Connection('localhost',27017) 
    dbh = self.__connection__['test_db']    
    collection = dbh['test'] 

如何通過1000塊讀取集合(以避免內存溢出,因爲集合可能非常大)?

回答

5

使用遊標。遊標有一個「batchSize」變量,用於控制在查詢後每個批次實際發送給客戶端的文檔數量。您不必觸摸這個設置,但是因爲默認情況很好,並且在大多數驅動程序中調用「getmore」命令的複雜性對您來說是隱藏的。我不熟悉pymongo但它的工作原理是這樣的:

cursor = db.col.find() // Get everything! 

while(cursor.hasNext()) { 
    /* This will use the documents already fetched and if it runs out of documents in it's local batch it will fetch another X of them from the server (where X is batchSize). */ 
    document = cursor.next(); 

    // Do your magic here 
} 
+1

你如何在Python中做到這一點? – 2017-04-20 05:17:27

5

我同意雷蒙,但你提到的1000批次,其中他的回答並沒有真正覆蓋。您可以設置光標一個批次大小:

cursor.batch_size(1000); 

您也可以跳過的記錄,例如:

cursor.skip(4000); 

這是你在找什麼?這實際上是一種分頁模式。但是,如果您只是想避免內存耗盡,那麼您並不需要設置批量大小或跳過。

0

到創建使用Pymongo目前在Python 2初始連接:

host = 'localhost' 
port = 27017 
db_name = 'test_db' 
collection_name = 'test' 

要使用MongoClient

# Connect to MongoDB 
client = MongoClient(host=host, port=port) 
# Make a query to the specific DB and Collection 
dbh = client[dbname] 
collection = dbh[collection_name] 

所以從這裏正確答案連接。 我想通過使用塊(在這種情況下的大小1000)閱讀。

chunksize = 1000 

例如,我們可以決定我們想要的塊的大小(塊大小)。

# Some variables to create the chunks 
skips_variable = range(0, db_aux[collection].find(query).count(), int(chunksize)) 
if len(skips_variable)<=1: 
    skips_variable = [0,len(skips_variable)] 

然後我們可以檢索每個塊。

for i in range(1,len(skips_variable)): 

    # Expand the cursor and retrieve data 

    data_from_chunk = dbh[collection_name].find(query)[skips_variable[i-1]:skips_variable[i]])) 

在這種情況下查詢是query = {}

Here我使用類似的想法從MongoDB創建數據框。 Here我使用類似的東西寫入MongoDB塊。

我希望它有幫助。