On this page本页内容
cursor.skip(<offset>)
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
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.这种方法在实现分页结果时可能很有用。
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 to include at least one field in your sort that contains unique values, before passing results to skip()
.skip()
与sort()
结合使用,请确保在将结果传递给skip()
之前,在排序中至少包含一个包含唯一值的字段。
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()
方法进行一致排序。
skip()
The following JavaScript function uses 以下JavaScript函数使用skip()
to paginate a collection by its _id
field:skip()
按集合的_id
字段为其分页:
function printStudents(pageNumber, nPerPage) { print( "Page: " + pageNumber ); db.students.find() .sort( { _id: 1 } ) .skip( pageNumber > 0 ? ( ( pageNumber - 1 ) * nPerPage ) : 0 ) .limit( nPerPage ) .forEach( student => { print( student.name ); } ); }
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
字段(即降序)大致按照最新文档的顺序排序:
function printStudents(startValue, nPerPage) { let endValue = null; db.students.find( { _id: { $lt: startValue } } ) .sort( { _id: -1 } ) .limit( nPerPage ) .forEach( student => { print( student.name ); endValue = student._id; } ); return endValue; }
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
从可能的最大键开始:
let currentKey = MaxKey; while (currentKey !== null) { currentKey = printStudents(currentKey, 10); }
Returning paginated results in ascending order is similar to the previous, but uses 以升序返回分页结果与前一个类似,但使用$gt
with an ascending sort order:$gt
和升序排序:
function printStudents(startValue, nPerPage) { let endValue = null; db.students.find( { _id: { $gt: startValue } } ) .sort( { _id: 1 } ) .limit( nPerPage ) .forEach( student => { print( student.name ); endValue = student._id; } ); return endValue; }
Using this function is likewise similar, but with 使用此功能类似,但以MinKey
as the starting key:MinKey
作为起始键:
let currentKey = MinKey; while (currentKey !== null) { currentKey = printStudents(currentKey, 10); }