gpt4 book ai didi

javascript - 如何在node js中使用mongoose加速查询mongodb

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:42 24 4
gpt4 key购买 nike

我是 MongoDB 和 Node.js 的新手。

我编写了一个示例应用程序来测量使用 Mongoose 向 MongoDB 发出获取请求的速度。

我有一个包含大约 200000 条记录的集合。在我的代码中,我想通过查询获取前 100000 行:

var query = db.myCollection.find().limit(100000);
query.exec(function(err, data){
// ....
});

大约花了99秒,我觉得速度太慢了。有人知道如何加快查询速度吗?

非常感谢!

最佳答案

使用 mongodb,您可以对某些键建立索引,以提高查询时的性能。但在这种情况下,这是行不通的。

您的 99 可能是由您的 PC 造成的,它无法一次性处理这种繁重的数据负载(这是完全可以理解的)。

这绝对与 mongodb 无关,而是与您的测试机内存相关。

但是你也许可以通过管道传输结果来改进它:

// use our lame formatter
var format = new ArrayFormatter;

// first pipe the querystream to the formatter
myCollection.find().stream().pipe(format);

// then pipe the formatter to the response
// (node 0.4x style pipe non-chaining)
format.pipe(res);

// In node 0.6 we can P.find().stream().pipe(format).pipe(res);

this gist

关于javascript - 如何在node js中使用mongoose加速查询mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com