2015-04-15 76 views
7

我通過如下查詢exceeds maximum document size problem例外,如何在不超過最大文檔大小的情況下編寫聚合?

pipe = [ 
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }} 
    ] 
res =db.patients.aggregate(pipe,allowDiskUse=True) 

我加入了$project運營商固定它,

但是如果文檔仍超過16MB即使我用$project

我該怎麼辦?任何想法 ?謝謝

pipe = [ 
    {"$project": {"birthday":1, "id":1} 
    }, 
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} } 
    } 
    ] 
res =db.patients.aggregate(pipe,allowDiskUse=True) 

異常

OperationFailure: command SON([('aggregate', 'patients'), ('pipeline', [{'$match': {'birthday': {'$gte': datetime.datetime(1987, 1, 1, 0, 0)}}}]), ('allowDiskUse', True)]) on namespace tw_insurance_security_development.$cmd failed: exception: aggregation result exceeds maximum document size (16MB) 

回答

24

默認情況下聚合的結果是一個BSON文件,這是在該尺寸的限制來自於退還給您。如果您需要返回更多,您可以:

  • 將結果輸出到集合。你用你完成管道做

    {「$了」:「一些收集名」}

    您然後查詢該集合爲正常(你需要自己刪除它,當你)

  • 將結果作爲遊標返回,在調用聚合時指定useCursor=True

這些選項都需要MongoDB的2.6:如果您仍在運行的MongoDB 2.4,那麼這是聚合的只是一個基本的限制。

+0

你會如此善良,給一個小的Java例子或至少一個來源?最好的 –

+0

對不起 - 我不熟悉Java mongo API –

+1

感謝您的快速反應。我找到了一個解決方案(使用spring數據和mongoDB): 'List pipeline = new ArrayList <>(); DBObject someMatchCriteria = new BasicDBObject(); someMatchCriteria.put(「param」,「value」); DBObject out = new BasicDBObject(); (「$ out」,「outCollectionName」); pipeline.add(新的BasicDBObject(「$ match」,someMatchCriteria)); pipeline.add(out); mongoOperations.getCollection(「inCollectionName」)。aggregate(pipeline);' –

-5

使用下面的代碼片段

db.patients.runCommand('aggregate', 
     {pipeline: [ 
    {"$project": {"birthday":1, "id":1}}, 
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }} 
], 
     allowDiskUse: true}) 

這裏allowDiskUse將有助於找出超過16點MB的數據

+0

實際上我已經啓用了'allowDiskUse'選項,但它仍然不起作用 – newBike

3

正如@Frederick說需要蒙戈2.6至少,對於進一步參考,here從蒙戈文檔的鏈接,其工作原理類似於runCommand方式,但使用db.collection.aggreagate,請注意,對於文檔限制使用「光標」選項,對於排序限制使用「allowDiskUse」選項。

相關問題