On this page本页内容
cursor.
limit
()¶mongo
Shell Method方法
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的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 the LIMIT
statement 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返回的结果超过处理所需的结果。
The limit()
method has the following prototype form:limit()
方法的原型形式如下:
The behavior of 对于小于-231且大于231的值,limit()
is undefined for values less than -231 and greater than 231.limit()
的行为未定义。
You must specify a numeric value for 必须为limit()
.limit()
指定一个数值。
A limit()
value of 0 (i.e. .limit(0)
) is equivalent to setting no limit.limit()
值为0(即limit(0)
)相当于不设置任何限制。
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
请求后续批处理。
limit()
with sort()
limit()
与sort()
一起使用¶If using 如果将limit()
with sort()
, be sure that you are performing a stable sort before passing results to limit()
. limit()
与sort()
一起使用,请确保在将结果传递给limit()
之前执行了稳定的排序。A stable sort ensures that the sort order of returned documents remains the same across multiple executions of the same sort; especially important when used with the 稳定的排序可确保在多次执行相同排序时,返回文档的排序顺序保持不变;与limit()
method.limit()
方法一起使用时尤其重要。
See Stable sorting with the sort() method for more information.有关更多信息,请参阅使用sort()
方法进行稳定排序。