Overview
在本指南中,您可以学习;了解如何使用MongoDB Rust驱动程序来执行限制操作。这些操作指定从读取操作返回的文档数。
使用  limit() 方法限制读取操作可以返回的文档数量。如果存在的文档数量不足以达到指定的限制,则此操作会返回较少的文档。
如果将 limit() 方法与 skip() 方法一起使用,则首先应用 skip,并且限制仅应用于其余文档。要学习;了解有关跳过操作的更多信息,请参阅 Skip Returned Results(跳过返回结果)指南。
示例样本数据
本指南中的示例使用以下 Book 结构作为 books 集合中文档的模型:
struct Book {     name: String,     author: String,     length: i32, } 
以下代码演示如何将示例数据插入 books集合:
let uri = "connection string"; let client = Client::with_uri_str(uri).await?; let my_coll: Collection<Book> = client.database("db").collection("books"); let books = vec![     Book {         id: 1,         name: "The Brothers Karamazov".to_string(),         author: "Dostoyevsky".to_string(),         length: 824,     },     Book {         id: 2,         name: "Atlas Shrugged".to_string(),         author: "Rand".to_string(),         length: 1088,     },     Book {         id: 3,         name: "Les Misérables".to_string(),         author: "Hugo".to_string(),         length: 1462,     },     Book {         id: 4,         name: "A Dance with Dragons".to_string(),         author: "Martin".to_string(),         length: 1104,     }, ]; my_coll.insert_many(books).await?; 
限制文档
您可以指定在查询或聚合管道中返回的最大文档数。
本节介绍如何通过以下方式限制结果:
- limit() 方法:将 - limit()方法链接到- find()方法
- FindOptions 结构体:使用 - limit选项
limit() 方法示例
要限制返回的文档数量,可以将 limit() 方法链接到 find() 方法。
此示例运行的 find() 操作会执行以下操作:
- 按 - length字段值的升序对结果进行排序
- 将结果限制为前三个文档 
let mut cursor = my_coll     .find(doc! {})     .sort(doc! { "length": 1 })     .limit(3).await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); } 
Book { name: "The Brothers Karamazov", author: "Dostoyevsky", length: 824 } Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } 
选项示例
或者,如果要设置和重复使用查询选项,则可以使用 FindOptions。使用 limit() 选项构建器方法设置 FindOptions 结构的 limit字段。然后,将 with_options() 方法链接到 find() 方法,并将 FindOptions 结构体作为参数传递给 with_options() 方法。
此示例运行的 find() 操作会执行以下操作:
- 筛选结果,仅包含 - length字段大于- 1000的文档
- 按 - length字段值的升序对结果进行排序
- 将结果限制为前两个文档 
let filter = doc! { "length": { "$gt": 1000 } }; let find_options = FindOptions::builder()     .sort(doc! { "length": 1 })     .limit(2)     .build(); let mut cursor = my_coll.find(filter).with_options(find_options).await?; while let Some(result) = cursor.try_next().await? {     println!("{:?}", result); } 
Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } 
聚合示例
您可以使用聚合管道中的 $limit 阶段来限制返回的结果。要学习;了解有关聚合操作的更多信息,请参阅 聚合指南。
此示例运行的聚合管道执行以下操作:
- 按 - length字段值的降序对结果进行排序
- 将返回的结果限制为前两个文档 
let pipeline = vec![     doc! { "$match": {} },     doc! { "$sort": { "length": -1 } },     doc! { "$limit": 2 }, ]; let mut cursor = my_coll.aggregate(pipeline).await?; while let Some(result) = cursor.try_next().await? {     println!("{:?}", result); } 
Document({"_id": Int32(3), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)}) Document({"_id": Int32(4), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)}) 
更多信息
要学习;了解有关本指南中提及的操作的更多信息,请参阅以下指南:
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: