Docs 菜单

Docs 主页开发应用程序MongoDB 驱动程序Java (Sync) 驱动程序

限制返回结果的数量

在此页面上

  • 概述
  • 指定限制
  • 结合使用 Skip 和 Limit

在本指南中,您可以了解如何使用 MongoDB Java 驱动程序限制从读取操作返回的结果数量。

使用 limit()来限制读取操作返回的文档数量。 此实例方法指定读取操作可以返回的最大文档数。 如果文档数量不足以达到指定的限制,则可以返回一个较小的数字。 如果将limit()skip()实例方法结合使用,则首先应用 skip,并且限制仅应用于在 skip 操作后剩余的文档。 有关skip()方法的更多信息,请参阅我们的“跳过返回的文档”指南。

以下示例分别演示如何向集合中插入数据,如何使用 limit() 限制返回的文档数量,以及如何将 limit()skip() 组合以进一步缩小查询返回的结果范围。

以下操作将表示书籍的文档插入到集合中:

collection.insertMany(Arrays.asList(
new Document().append("_id", 1)
.append("title", "The Brothers Karamazov").append("length", 824)
.append("author", "Dostoyevsky"),
new Document().append("_id", 2)
.append("title", "Les Misérables").append("length", 1462).append("author", "Hugo"),
new Document().append("_id", 3)
.append("title", "Atlas Shrugged").append("length", 1088).append("author", "Rand"),
new Document().append("_id", 4)
.append("title", "Infinite Jest").append("length", 1104).append("author", "Wallace"),
new Document().append("_id", 5)
.append("title", "Cryptonomicon").append("length", 918).append("author", "Stephenson"),
new Document().append("_id", 6)
.append("title", "A Dance with Dragons").append("length", 1104)
.append("author", "Martin")
));

下一个示例会查询集合以返回长度排名前三的图书。它首先将所有文档与查询相匹配,然后对 length 字段进行排序,以先返回长度较长的书籍,然后返回长度较短的书籍。最后,它将返回值限制为 3 个文档:

import com.mongodb.client.*;
import org.bson.Document;
import static com.mongodb.client.model.Sorts.descending;
// ...
// define a cursor that will return the first 3 sorted items
MongoCursor<Document> cursor = collection.find()
.sort(descending("length"))
.limit(3)
.iterator();
// print out items
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
// close the cursor
finally {
cursor.close();
}

前面的代码示例打印以下三份文档,按长度排序:

Document{{_id=2, title=Les Misérables, author=Hugo, length=1462}}
Document{{_id=6, title=A Dance with Dragons, author=Martin, length=1104}}
Document{{_id=4, title=Infinite Jest, author=Wallace, length=1104}}

提示

调用 limit()sort() 的顺序并不重要,因为驱动程序会对调用重新排序以首先应用排序,然后再应用限制。以下两个调用是等效的:

collection.find().sort(descending("length")).limit(3);
collection.find().limit(3).sort(descending("length"));

要查看接下来的三本最长的书,请将 skip() 方法附加到您的 find() 调用中,如以下代码示例所示:

MongoCursor<Document> cursor = collection.find()
.sort(descending("length"))
.limit(3)
.skip(3)
.iterator();

此操作返回说明第四至第六长书籍的文档:

Document{{_id=3, title=Atlas Shrugged, author=Rand, length=1088}}
Document{{_id=5, title=Cryptonomicon, author=Stephenson, length=918}}
Document{{_id=1, title=The Brothers Karamazov, author=Dostoyevsky, length=824}}

您可以通过这种方式,结合 skip()limit() 来为您的集合实现分页,一次仅返回集合的小“切片”。

注意

为了确保在多个查询中稳定排序,必须使用唯一键(例如_id )进行排序。 否则,当调用skip()limit()sort()结合使用时,可能会产生不可预测的结果。

例如,考虑以下数据:

{ type: "computer", data: "1", serial_no: 235235 }
{ type: "computer", data: "2", serial_no: 235237 }
{ type: "computer", data: "3", serial_no: 235239 }
{ type: "computer", data: "4", serial_no: 235241 }

如果仅按type排序, sort()并不能保证返回时顺序相同。 将skip()limit()附加到sort()可以为不同的查询返回不同的文档。 在这种情况下,按dataserial_no排序将保证稳定排序,因为两者都是唯一键。

有关本指南中提到的方法和类的详情,请参阅以下 API 文档:

  • FindIterable

  • MongoIterable

  • MongoCursor

  • find()

← 跳过返回的结果