节点。js阻塞mongodb调用

时间:2022-10-23 20:27:27

I am relatively new to Node.js and working on a project with MongoDB as database. Some of the MongoDB queries are expensive. For example, I have a collection with millions of records and based on the search criteria the find query can take 1-2 seconds.

我对Node比较陌生。并以MongoDB为数据库进行项目开发。MongoDB的一些查询非常昂贵。例如,我有一个包含数百万条记录的集合,根据搜索条件,查找查询可能需要1-2秒。

Does the call to MongoDB (using Mongoose driver) block Node.js from processing other requests until MongoDB returns results?

调用MongoDB(使用Mongoose驱动程序)块节点。从处理其他请求到MongoDB返回结果?

Also, are there any tools to identify potential lines of code that are of a blocking nature?

此外,有什么工具可以识别具有阻塞性质的潜在代码行吗?

Thank you.

谢谢你!

1 个解决方案

#1


3  

@user949300 provided correct answer.

@user949300提供正确答案。

In general, if they have a callback in the function signature they ar non-blocking. e.g. MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) has a callback function(err, db) and is non- blocking.

通常,如果它们在函数签名中有回调,它们就是非阻塞的。连接('mongodb:// 127.0.1:27 017/test',函数(err, db)具有回调函数(err, db),并且不阻塞。

#1


3  

@user949300 provided correct answer.

@user949300提供正确答案。

In general, if they have a callback in the function signature they ar non-blocking. e.g. MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) has a callback function(err, db) and is non- blocking.

通常,如果它们在函数签名中有回调,它们就是非阻塞的。连接('mongodb:// 127.0.1:27 017/test',函数(err, db)具有回调函数(err, db),并且不阻塞。