On this page本页内容
cursor.
skip
(<offset>)¶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驱动程序文档。
Call the 对游标调用skip()
method on a cursor to control where MongoDB begins returning results. skip()
方法,以控制MongoDB开始返回结果的位置。This approach may be useful in implementing paginated results.这种方法可能有助于实现分页结果。
Note
You must apply 在从数据库检索任何文档之前,必须对游标应用skip()
to the cursor before retrieving any documents from the database.skip()
。
The skip()
method has the following parameter:skip()
方法具有以下参数:
offset |
number |
skip()
with sort()
skip()
与sort()
一起使用¶If using 如果将skip()
with sort()
, be sure that you are performing a stable sort before passing results to skip()
. skip()
与sort()
一起使用,请确保在将结果传递给skip()
之前执行了稳定的排序。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 稳定的排序可确保在多次执行相同排序时,返回文档的排序顺序保持不变;与skip()
method.skip()
方法一起使用时尤其重要。
See Stable sorting with the sort() method for more information.有关更多信息,请参阅使用sort()
方法进行稳定排序。
skip()
¶The following JavaScript function uses 以下JavaScript函数使用skip()
to paginate a collection by its _id
field:skip()
按集合的_id
字段对集合进行分页:
The skip()
method requires the server to scan from the beginning of the input results set before beginning to return results. skip()
方法要求服务器在开始返回结果之前,从输入结果集的开头开始扫描。As the offset increases, 随着偏移量的增加,skip()
will become slower.skip()
将变慢。
Range queries can use indexes to avoid scanning unwanted documents, typically yielding better performance as the offset grows compared to using 范围查询可以使用索引来避免扫描不需要的文档,与使用skip()
for pagination.skip()
分页相比,随着偏移量的增加,通常会产生更好的性能。
Use this procedure to implement pagination with range queries:使用以下步骤可以使用范围查询实现分页:
_id
which generally changes in a consistent direction over time and has a unique index to prevent duplicate values,_id
,该字段通常随时间以一致的方向变化,并具有唯一的索引以防止重复值,$lt
and sort()
operators, and$lt
和sort()
运算符查询字段小于起始值的文档,以及For example, the following function uses the above procedure to print pages of student names from a collection, sorted approximately in order of newest documents first using the 例如,以下函数使用上述过程打印集合中的学生姓名页面,首先使用_id
field (that is, in descending order):_id
字段(即降序)按最新文档的大致顺序排序:
You may then use the following code to print all student names using this pagination function, using 然后,您可以使用以下代码使用此分页功能打印所有学生姓名,使用MaxKey
to start from the largest possible key:MaxKey
从可能的最大键开始: