繁体   English   中英

如何在Node.js中使用猫鼬加快查询Mongdb

[英]How to speed up query mongdb with mongoose in nodejs

我是MongoDB和Node.js的新手。

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

我有大约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);

有了这个要点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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