AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ソート ビルダ

このガイドでは、 Java Reactive Streams ドライバーのビルダを使用してクエリのソート基準を指定する方法を学びます。

ソート基準は、MongoDB がデータをソートするために使用するルールです。 ソート条件の例は、次のとおりです。

  • 最小数から最大数へ

  • 時刻までの時刻から最新時刻まで

  • 名がアルファベット順に並べられている

ビルダはJava Reactive Streams ドライバーによって提供されるクラスで、 BSONオブジェクトの構築に役立ちます。詳細については、 ビルダガイドを参照してください。

Tip

簡潔にするために、Sortクラスのすべてのメソッドを静的にインポートすることを選択できます。

import static com.mongodb.client.model.Sorts.*;

次の例では、この静的インポートを前提としています。

このページの例では、次のドキュメントを含むサンプル コレクションを使用します。

{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" },
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" },
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" },
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" },
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" },
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }

Sortsクラスは、 MongoDBでサポートされているすべての並べ替え条件演算子の静的ファクトリー メソッドを提供するビルダです。これらのメソッドはBson sort()FindPublisherAggregates.sort()オブジェクトを返します。これは、 インスタンスの メソッドまたは に渡すことができます。Aggregates クラスの詳細については、 集計ビルダガイドを参照してください。

このセクションのクラスとインターフェースの詳細については、次の API ドキュメントを参照してください。

昇順の並べ替えを指定するには、 Sorts.ascending()静的ファクトリー メソッドを使用します。 並べ替えるフィールドの名前をSorts.ascending()に渡します。

次の例では、サンプル コレクション内のドキュメントを_idフィールドの昇順でソートします。

FindPublisher<Document> findPublisher = collection.find().sort(ascending("_id"));
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" }
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" }
...

降順の並べ替えを指定するには、 Sorts.descending()静的ファクトリー メソッドを使用します。 並べ替えるフィールドの名前をSorts.descending()に渡します。

次の例では、サンプル コレクション内のドキュメントを_idフィールドで降順にソートします。

FindPublisher<Document> findPublisher = collection.find().sort(descending("_id"));
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" }
...

並べ替え条件を結合するには、 Sorts.orderBy()静的ファクトリー メソッドを使用します。 このメソッドは、ソート条件の順序付きリストを含むオブジェクトを構築します。 ソートを実行する際に、左端のソート条件が同値になった場合、ソートはリスト内の次のソート条件を使用して順序を決定します。

次の例では、サンプルコレクション内のドキュメントをdateフィールドで降順でソートし、同点の場合はorderTotalフィールドで昇順にソートします。

Bson orderBySort = orderBy(descending("date"), ascending("orderTotal"));
FindPublisher<Document> findPublisher = collection.find().sort(orderBySort);
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" }
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" }
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" }
{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }

テキスト スコアは、検索結果が検索 string と一致する可能性を高めるために使用されるテキスト クエリで並べ替えることができます。テキストクエリのテキストスコアで並べ替えを指定するには、Sorts.metaTextScore() 静的ファクトリー メソッドを使用します。

詳細については、「 ソートクラスのAPIドキュメント 」を参照してください。