2017-04-06 143 views
0

當我嘗試從mongodb集合中獲取數據時,出現以下異常。這個集合有非常大的數據。Java與mongodb

唯一的例外是:

com.mongodb.MongoQueryException: Query failed with error code 10334 and error message 'BSONObj size: 24020168 (0x16E84C8) is invalid. Size must be between 0 and 16793600(16MB)' on server 10.15.0.227:27017 

而下面是我的查詢這是我用來從MongoDB中獲取數據:

db.getCollection('triggered_policies').aggregate(
[{ "$match" : { "policy_name" : "EIQSOC-1040-ec"}}, 
{ "$project" : { "cust_created_at" : { "$add" : [ "$created_at" , 19800000]} , "event_ids" : "$event_ids" , "trigger_time" : "$trigger_time" , "created_at" : "$created_at" , "triggered_rules" : "$triggered_rules"}}, 
{ "$sort" : { "created_at" : -1}}, 
{ "$group" : 
    { "_id" : 
     { 
      "$hour" : "$cust_created_at"} , 
      "triggered_policies" : { "$addToSet" : { "trigger_time" : "$trigger_time" , "created_at" : "$created_at" , "event_ids" : "$event_ids" , "triggered_rules" : "$triggered_rules"} 
     } 
    } 
}, 
{ "$sort" : { "_id" : 1}} 
]) 

下面是我們得到的確切異常:

Error: getMore command failed: { 
    "ok" : 0, 
    "errmsg" : "BSONObj size: 25994482 (0x18CA4F2) is invalid. Size must be between 0 and 16793600(16MB)", 
    "code" : 10334 
} 

請幫助我們解決問題。

回答

0

看起來像聚合過程中創建的文檔超過了mongo db中的16MB大小限制。您可能必須更改聚合查詢,以便不會將太多數據累積到超過16MB大小限制的單個文檔中。

下面是蒙戈DB文檔報價:

BSON文檔大小 最大BSON文件大小爲16兆字節。 最大文檔大小有助於確保單個文檔不能使用過量的RAM,或者在傳輸過程中使用過多的帶寬。爲了存儲大於最大大小的文檔,MongoDB提供了GridFS API。有關GridFS的更多信息,請參閱mongofiles和您的驅動程序的文檔。

+0

在created_at上定義索引應該有助於減少聚合過程中操縱的數據大小。 –

+0

謝謝JayKrish的快速回復。 但幫我準備那種查詢。 –

+0

問題出在$ group,因爲您使用$ addToSet,它會將所有唯一數據添加到數組並增加文檔大小。相反,嘗試從這些文檔中只獲取所需的值。 – JayKrish