简体   繁体   English

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

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

I'm a newbie in MongoDB and Node.js. 我是MongoDB和Node.js的新手。

I wrote an example app to measure how fast a get request to MongoDB using Mongoose. 我编写了一个示例应用程序,用于测量使用Mongoose向MongoDB发出get请求的速度。

I have a collection with about 200000 records. 我有大约200000条记录的集合。 In my code, I want to get the first 100000 rows by query: 在我的代码中,我想通过查询获取前100000行:

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

and it took about 99s, I think that speed was too slow. 大约花了99秒钟,我认为速度太慢了。 Does anyone have ideas on how to speed up the query? 有人对加快查询速度有想法吗?

Thank you so much! 非常感谢!

With mongodb, you can index some of your keys to improve the performance when you're querying. 使用mongodb,您可以在查询时索引一些键以提高性能。 But in this case, this will not work. 但是在这种情况下,这将不起作用。

Your 99s are probably caused by your PC which just can't handle this kind of heavy load of data in a single time (that's perfectly understandable). 您的99年代可能是由您的PC引起的,它无法一次处理这种繁重的数据负载(这是完全可以理解的)。

That is absolutely not in correlation with mongodb but with your testing machine memory. 那绝对与mongodb不相关,而与测试机器的内存相关。

But you maybe can improve it by piping the result : 但是您可以通过传递结果来改善它:

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

With this gist 有了这个要点

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

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