简体   繁体   English

具有NodeJS驱动程序的MongoDB聚合游标

[英]MongoDB Aggregation Cursor with NodeJS Driver

I am using MongoDB v3.2 and I'm using the native nodejs driver v2.1. 我正在使用MongoDB v3.2,并且正在使用本机nodejs驱动程序v2.1。 When running the aggregation pipeline on large data sets(1mil+ documents), I am encountering the following error: 在大型数据集(超过1mil个文档)上运行聚合管道时,遇到以下错误:

 'aggregation result exceeds maximum document size (16MB)'

Here is my aggregation pipeline code: 这是我的聚合管道代码:

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 () {

            });

Things I've tried: 我尝试过的事情:

//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) {

})

I've seen in other answers ( How could I write aggregation without exceeds maximum document size? ) that I need to have the results returned by a cursor, so how do I properly do that? 我在其他答案( 如何不超过最大文档大小的情况下编写聚合 )中看到,我需要让游标返回结果,那么我该如何正确地做到这一点? I just can't seem to get it to work. 我似乎无法正常工作。 Any suggestions on what the batchSize should be? 关于batchSize应该是什么建议?

I am using the native mongodb package - https://github.com/mongodb/node-mongodb-native for a nodejs project not the mongo command line. 我正在使用本机mongodb软件包-https: //github.com/mongodb/node-mongodb-native用于nodejs项目而不是mongo命令行。

Ok I figured it out. 好吧,我知道了。 It was not working because I was passing in a callback function as the last parameter in the aggregate method. 它不起作用,因为我将回调函数作为聚合方法中的最后一个参数传入。 By passing null, it allowed the stream to work as expected. 通过传递null,它允许流按预期工作。 Changes shown below: 更改如下所示:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM