简体   繁体   English

Mongodb 查询如何在 node.js 中工作?

[英]How Mongodb queries works in node.js?

I have a very easy search query in Node.js Express.js MongoDB with Mongoose:我在 Node.js Express.js MongoDB 和 Mongoose 中有一个非常简单的搜索查询:

await Model.find({}).limit(10);

My question is how do the architects work?我的问题是建筑师如何工作? Is it first to get all Models Data and then limit to 10 or before getting all data will select 10 items from the database?是先获取所有模型数据然后限制为 10 还是在获取所有数据之前从数据库中获取 select 10 项? I mean the steps:我的意思是步骤:

  1. Find all data from Model and return as List(Array) --> 2. Limit 10 first items and remove others from List(Array).从 Model 中查找所有数据并返回为 List(Array) --> 2. 限制 10 个第一项并从 List(Array) 中删除其他项。

  2. Find first 10 items and return as List(Array)查找前 10 个项目并以 List(Array) 形式返回

The difference in performance is high cause with first step if we got a million data it will return 1 mill items with a huge 10 20 sec and then limiting the 10 of it which we loose 10 20 seconds of time and when the user are more the server will be done but with the second way even with 100 mil items it will always take same time.性能差异很大,第一步是如果我们获得一百万个数据,它将以巨大的 10 20 秒返回 1 个研磨项目,然后限制其中的 10 个,我们失去 10 20 秒的时间,当用户更多时服务器将完成,但第二种方式即使有 1 亿个项目,也总是需要相同的时间。

The limit function sets specifies the maximum number of elements a cursor will return. function 设置的limit指定了cursor将返回的最大元素数。 In the case of your example, the cursor will return the first 10 items matching the query only (option 2).在您的示例中,cursor 将仅返回与查询匹配的前 10 个项目(选项 2)。 You can find more information on how the cursor.limit() works via the links below:您可以通过以下链接找到有关cursor.limit()如何工作的更多信息:

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

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