排序结果
概述
在本指南中,您可以了解如何使用MongoDB Rust驱动程序执行排序操作以指定读取操作结果的顺序。
当构建选项以更改读取操作返回文档的顺序时,请使用sort()
方法。该sort()
方法告诉MongoDB按一个或多个字段的值以特定方向对返回的文档进行排序。要按升序(最低首先)对返回的文档进行排序,请使用值1
。要按降序(最高首先)排序,则使用-1
。如果不指定排序,MongoDB不保证查询结果的顺序。
示例数据
本指南中的示例使用以下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?;
排序方法
您可以按查询检索到的结果排序,或者对聚合管道中的结果进行排序。
将 sort()
方法链接到 find()
方法以按查询检索到的结果排序,如下例所示
let mut cursor = my_coll .find(doc! {}) // 1 for ascending order, -1 for descending order .sort(doc! { "author": 1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
选项
或者,您可以使用 FindOptions
结构的 sort()
方法。
以下示例执行了具有以下行为的 find()
操作
按
author
字段的值以升序排序结果跳过第一个文档
返回剩余的文档
let find_options = FindOptions::builder() // 1 for ascending order, -1 for descending order .sort(doc! { "author": 1 }) .skip(1) .build(); let mut cursor = my_coll.find(doc! {}).with_options(find_options).await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
聚合
要按聚合管道中的结果进行排序,创建一个 $sort
阶段并将阶段列表传递给 aggregate()
方法。
以下示例显示了如何创建一个按 author
字段的值以升序对文档进行排序的 $sort
阶段
let pipeline = vec![ doc! { "$match": {} }, // 1 for ascending order, -1 for descending order doc! { "$sort": { "author": 1 } } ]; let mut cursor = my_coll.aggregate(pipeline).await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
排序方向
您的排序方向可以是 升序 或 降序。升序排序将结果从最小到最大排序。降序排序将结果从最大到最小排序。
以下列表包含按升序排序的数据示例
数字:1, 2, 3, 43, 43, 55, 120
日期:1990-03-10, 1995-01-01, 2005-10-30, 2005-12-21
单词(ASCII):Banana, Dill, carrot, cucumber, hummus
以下列表包含按降序排序的数据示例
数字:100, 30, 12, 12, 9, 3, 1
日期:2020-01-01, 1998-12-11, 1998-12-10, 1975-07-22
单词(反向ASCII):pear, grapes, apple, Cheese
以下小节展示了如何指定这些排序标准。
升序
要指定升序排序,请将您想要排序的字段和 1
传递给 sort()
方法。
示例
以下示例指定了在 name
字段上执行升序排序
let mut cursor = my_coll .find(doc! {}) .sort(doc! { "name": 1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Book { "_id": 4, "name": "A Dance with Dragons", "author": Martin, "length": 1104 } Book { "_id": 2, "name": "Atlas Shrugged", "author": Rand, "length": 1088 } Book { "_id": 3, "name": "Les Miserables", "author": Hugo, "length": 1462 } Book { "_id": 1, "name": "The Brothers Karamazov", "author": Dostoevsky, "length": 824 }
降序
要指定降序排序,将需要排序的字段和 -1
传递给 sort()
方法。
示例
以下示例指定对 name
字段进行降序排序
let mut cursor = my_coll .find(doc! {}) .sort(doc! { "name": -1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Book { "_id": 1, "name": "The Brothers Karamazov", "author": Dostoevsky, "length": 824 } Book { "_id": 3, "name": "Les Miserables", "author": Hugo, "length": 1462 } Book { "_id": 2, "name": "Atlas Shrugged", "author": Rand, "length": 1088 } Book { "_id": 4, "name": "A Dance with Dragons", "author": Martin, "length": 1104 }
更多信息
有关本指南中提到的操作的更多信息,请参阅以下内容
API 文档
有关本指南中讨论的任何方法或类型的更多信息,请参阅以下 API 文档