nodejs - mongodb 本机查找所有文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21626896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:49:13  来源:igfitidea点击:

nodejs - mongodb native find all documents

node.jsmongodb

提问by dopplesoldner

Following an example from the mongodb manual for nodejs, I am finding all documents from a db as follows

按照 mongodb 手册中 nodejs 的示例,我从 db 中查找所有文档,如下所示

mongo.Db.connect(mongoUri, function (err, db) {
    if (err) {
        console.log(err);
    } 
    else {
        db.collection('test').find().toArray(function(e, d) {
            console.log(d.length);
            db.close();
        });
    }
});

Now what I notice is that the entire set is converted to an array. As the dataset will grow, this will not be the ideal approach. Is there anyway to streamthe data so it is not loaded in memory every time?

现在我注意到的是整个集合被转换为一个数组。随着数据集的增长,这将不是理想的方法。无论如何都要流式传输数据,以便每次都不会将其加载到内存中?

Thanks

谢谢

回答by WiredPrairie

The easiest way is to use a Cursor(reference):

最简单的方法是使用Cursor参考):

var cursor = db.collection('test').find();

// Execute the each command, triggers for each document
cursor.each(function(err, item) {
    // If the item is null then the cursor is exhausted/empty and closed
    if(item == null) {
        db.close(); // you may not want to close the DB if you have more code....
        return;
    }
    // otherwise, do something with the item
});

If there's a lot of computation you need to do, you might consider whether a Map-Reduce (reference) would fit your needs as the code would execute on the DB server, rather than locally.

如果您需要进行大量计算,您可能会考虑 Map-Reduce(参考)是否适合您的需求,因为代码将在数据库服务器上执行,而不是在本地执行。

回答by JohnnyHK

You can stream the results of a node.js native driver's query by calling stream()on the returned cursor:

您可以通过调用stream()返回的游标来流式传输 node.js 本机驱动程序的查询结果:

var stream = collection.find().stream();
stream.on('data', function(doc) {
    console.log(doc);
});
stream.on('error', function(err) {
    console.log(err);
});
stream.on('end', function() {
    console.log('All done!');
});

回答by d1ll1nger

Would limiting the query be an option? Literally with db.collection.find().limit()? The limit is parsed before sending the command to the server, so it only scans the amount of data in your limit.

限制查询是一种选择吗?从字面上看 db.collection.find().limit()?在将命令发送到服务器之前解析限制,因此它只扫描限制中的数据量。