2017-12-11 10 views
0

ネストされた人口を持つMongooseコールを実行していて、データに対して何らかの処理を行っています。Mongoose Populateで16MB BSON制限に達する

しかし、ネストされたポピュレートコールのため、私はMongooseでBSONドキュメントの16MB制限に達しています。

この問題を解決する方法はありますか。

let allDocuments = await exampleModel.find({ 
    condition: true 
    }).populate({path: 'tvShows', populate: {path: 'views'}}); 

enter image description here

たぶん私は、複数の呼び出しにに破ることができますか?しかし、私はそれを行うための論理的な方法は不明です。ありがとう。

答えて

1

ページングを実装するには、skiplimitクエリパラメータを使用します。

const query = yourModel.find({ /* your conditions here */ }); 
const batchSize = 100; 

function mergeAllReducer(accumulator, currentValue) { 
    return accumulator.concat(currentValue); 
} 

query.count().then(total => { 
    let skip = 0; 
    const allQueries = []; 
    while (skip < total) { 
    allQueries.push(query.find() 
     .skip(skip) 
     .limit(batchSize) 
     .populate({ path: 'tvShows', populate: { path: 'views' }}) 
    ); 
    skip += batchSize; 
    } 
    return Promise.all(allQueries); 
}) 
.then(arrayOfArrays => arrayOfArrays.reduce(mergeAllReducer, [])) 
.then(result => { 
    // do something with your populated result 
}); 

、あなたはまだこのすべてのメモリが使用されて対処する必要があります、あなたはおそらく、同様のバッチで処理して試みることができるので、あなたのjavascript配列のサイズはまだ、あなたVMが処理できるよりも大きくなる場合がございますのでご了承ください配列全体を操作する代わりに、

+0

ありがとうございました。私は同様の設定をしました。16MBの制限を引き上げる方法はありましたが、何でも構いません。 – Antoine

+1

通常、無制限の量のデータをメモリに読み込むことはできません。チャンク内のデータを読み取ることは常に良い方法です。あなたが一度にすべてを処理する必要がある場合でも。バッチ・リードを使用できないユース・ケースを考えるのは非常に難しいです。 – Zilvinas

関連する問題