[英]MongoDB Aggregation Cursor with NodeJS Driver
我正在使用MongoDB v3.2,並且正在使用本機nodejs驅動程序v2.1。 在大型數據集(超過1mil個文檔)上運行聚合管道時,遇到以下錯誤:
'aggregation result exceeds maximum document size (16MB)'
這是我的聚合管道代碼:
var eventCollection = myMongoConnection.db.collection('events');
var cursor = eventCollection.aggregate([
{
$match: {
event_type_id: {$eq: 89012}
}
},
{
$group: {
_id: "$user_id",
score: {$sum: "$points"}
}
},
{
$sort: {
score: -1
}
}
],
{
cursor: {
batchSize: 500
},
allowDiskUse: true,
explain: false
}, function () {
});
我嘗試過的事情:
//Using cursor event listeners. None of the on listeners seem to work. Always get error about 16mb.
cursor.on("data", function (data) {
console.log("Some data: ", data);
});
cursor.on("end", function (data) {
console.log("End of data: ", data);
});
//Using forEach. Which I thought would allow for >16mb because it's used in conjunction with the batchSize and cursor.
cursor.forEach(function (item) {
})
我在其他答案( 如何在不超過最大文檔大小的情況下編寫聚合 )中看到,我需要讓游標返回結果,那么我該如何正確地做到這一點? 我似乎無法正常工作。 關於batchSize應該是什么建議?
我正在使用本機mongodb軟件包-https: //github.com/mongodb/node-mongodb-native用於nodejs項目而不是mongo命令行。
好吧,我知道了。 它不起作用,因為我將回調函數作為聚合方法中的最后一個參數傳入。 通過傳遞null,它允許流按預期工作。 更改如下所示:
var cursor = eventCollection.aggregate([
{
$match: {
event_type_id: {$eq: 89012}
}
},
{
$group: {
_id: "$user_id",
score: {$sum: "$points"}
}
},
{
$sort: {
score: -1
}
}
],
{
cursor: {
batchSize: 500
},
allowDiskUse: true,
explain: false
}, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.