Definition定义
cursor.limit()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Use the在游标上使用limit()method on a cursor to specify the maximum number of documents the cursor will return.limit()方法指定游标将返回的最大文档数。limit()is analogous to theLIMITstatement in a SQL database.limit()类似于SQL数据库中的LIMIT语句。Note
You must apply在从数据库检索任何文档之前,必须对游标应用limit()to the cursor before retrieving any documents from the database.limit()。Use使用limit()to maximize performance and prevent MongoDB from returning more results than required for processing.limit()来最大限度地提高性能,并防止MongoDB返回的结果超过处理所需的结果。Thelimit()method has the following prototype form:limit()方法具有以下原型形式:db.collection.find(<query>).limit(<number>)
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Behavior行为
Supported Values支持的值
The behavior of 对于小于-231且大于231的值,limit() is undefined for values less than -2 31 and greater than 2 31.limit()的行为未定义。
You must specify a numeric value for 您必须为limit().limit()指定一个数值。
Zero Value零值
A limit() value of 0 (i.e. .limit(0)) is equivalent to setting no limit.limit()值为0(即limit(0))相当于不设置限制。
Negative Values负值
A negative limit is similar to a positive limit but closes the cursor after returning a single batch of results. 负极限类似于正极限,但在返回单批结果后会关闭游标。As such, with a negative limit, if the limited result set does not fit into a single batch, the number of documents received will be less than the specified limit. 因此,在负限制的情况下,如果有限的结果集不适合单个批次,则收到的文档数量将小于指定的限制。By passing a negative limit, the client indicates to the server that it will not ask for a subsequent batch via 通过传递负限制,客户端向服务器指示它不会通过getMore.getMore请求后续批处理。
Using limit() with sort()使用limit()和sort()
limit() with sort()If using 如果将limit() with sort(), be sure to include at least one field in your sort that contains unique values, before passing results to limit().limit()与sort()一起使用,请确保在将结果传递给limit()之前,在排序中至少包含一个包含唯一值的字段。
Sorting on fields that contain duplicate values may return an inconsistent sort order for those duplicate fields over multiple executions, especially when the collection is actively receiving writes.对包含重复值的字段进行排序可能会在多次执行中为这些重复字段返回不一致的排序顺序,特别是在集合正在积极接收写入时。
The easiest way to guarantee sort consistency is to include the 保证排序一致性的最简单方法是在排序查询中包含_id field in your sort query._id字段。
See Consistent sorting with the sort() method for more information.有关更多信息,请参阅使用sort()方法进行一致排序。
Using limit() with skip()使用limit()和skip()
limit() with skip()When you chain 当你链接skip() and limit(), the method chaining order does not affect the results. skip()和limit()时,方法链接顺序不会影响结果。The server always applies the skip operation based on the sort order before it applies the limit on how many documents to return.服务器在应用要返回的文档数量限制之前,始终根据排序顺序应用跳过操作。
The following code example shows different chaining orders for 以下代码示例显示了skip() and limit() that always produce the same query results for the same data set:skip()和limit()的不同链接顺序,它们总是为同一数据集生成相同的查询结果:
db.myColl.find().sort({_id: 1}).skip(3).limit(6);
db.myColl.find().sort({_id: 1}).limit(6).skip(3);