2015-09-23 54 views
1

我想在pymongo的幫助下,使用python中的以下代碼行來獲取所有具有祖先名稱的「Laptops」文檔。MongoDb在搜索時給出錯誤

for p in collection.find({"ancestors.name":"Laptops"}): 
    print p 

但我得到這個錯誤。

pymongo.errors.OperationFailure: database error: BSONObj size: 536871080 (0x200000A8) is invalid. Size must be between 0 and 16793600(16MB) First element: seourl: "https://example.com" 

如果我限制查詢作爲

for p in collection.find({"ancestors.name":"Laptops"}).limit(5): 
    print p 

然後,它的工作原理。所以我想這個問題是在獲取這個類別的所有文檔時。如何解決這個問題呢?我需要「筆記本電腦」的所有文件。

編輯: -

採用聚集的管道概念我想下面的查詢

db.product_attributes.aggregate([ 
{ 
    $match: 
    { 
     "ancestors.name":"Laptops" 
    } 
    } 
]) 

我得到同樣的錯誤

uncaught exception: aggregate failed: { 
    "errmsg" : "exception: BSONObj size: 536871080 (0x200000A8) is invalid. Size must be between 0 and 16793600(16MB) First element: seourl: \"https://example.com"", 
    "code" : 10334, 
    "ok" : 0 
} 

請告訴我錯在這裏..?幫助表示讚賞:)

回答

1

查詢返回的文檔的最大大小爲16MB。你可以看到,並在official document

其他限制爲了克服這一點,你可以指望的記錄總數和環比的記錄,並打印出來

樣品:

count=db.collection.count({"ancestors.name":"Laptops"}) 
for num in range (0,count,500): 
    if num!=0: 
     for p in collection.find({"ancestors.name":"Laptops"}).skip(num-1).limit(500): 
       print p 
     else: 
     for p in collection.find({"ancestors.name":"Laptops"}).limit(500): 
       print p 

警告:

由於您跳過並限制記錄,此方法速度較慢

+1

這仍然給出了錯誤:( – Gunjan

+0

@Gunjan你嘗試改變極限的東西不怎麼樣100 – The6thSense

+0

是的,我做了分至10但還是同樣的問題。 – Gunjan

1

限制的原因是不允許您的mongoDB進程在服務器上使用所有內存。要了解更多信息,請參閱ticket大約增加4到16 MB的限制並討論它的用途。

替代的方法是使用Aggregation pipeline

如果聚合命令返回包含 完整結果集的單個文件,如果結果 集超過BSON文檔尺寸限制該命令將產生一個錯誤,目前是16 兆字節。要管理超出此限制的結果集,如果命令返回一個 遊標或將結果存儲到集合,則合計 命令可以返回任意大小的結果集。

+0

嘗試此查詢「db.product_attributes.aggregate([{」$ match「:{」ancestors.name「:」Laptops「}}])」。仍然得到相同的錯誤 – Gunjan

相關問題