Overview
このガイドでは、MongoDB Kotlin ドライバー のビルダを使用してクエリのソート基準を指定する方法を学びます。
ソート基準は、MongoDB がデータをソートするために使用するルールです。 ソート条件の例は、次のとおりです。
最小数から最大数へ
時刻までの時刻から最新時刻まで
名がアルファベット順に並べられている
ビルダは Kotlin ドライバーによって提供されるクラスで、 BSONオブジェクトの構築に役立ちます。 詳細については、ビルダのガイド を参照してください。
ビルダを使用してクエリのソート基準を指定する方法について詳しはは、こちらのガイドをお読みください。
Kotlin ドライバーでのソートの基礎については、「ソートガイド 」を参照してください。
このページの例では、次のドキュメントを含むサンプル コレクションを使用します。
{ "_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 vanilla birthday cakes" }, { "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen vanilla 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": 60.31, "description": "one large vanilla and chocolate cake" }, { "_id": 6, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
このデータは、次の Kotlin データ クラスでモデル化されます。
data class Order( val id: Int, val date: String, val orderTotal: Double, val description: String, )
Tip
ビルダ メソッドとデータ クラスのプロパティ
任意のKotlinドライバー拡張の依存関係をアプリケーションに追加することで、ビルダ クラスのメソッドをデータクラスプロパティで直接使用できます。 詳細と例については、「 データ クラスを使用したビルダの使用 」ガイドを参照してください。
sorts クラス
Sortsクラスは、MongoDB でサポートされているすべての並べ替え条件演算子の静的ファクトリー メソッドを提供するビルダです。 これらのメソッドはBsonオブジェクトを返します。これは、 FindFlowインスタンスのsort()メソッドまたはAggregates.sort()に渡すことができます。
Aggregatesクラスの詳細については、集計ビルダガイドを参照してください。
このセクションのクラスとインターフェースの詳細については、次の API ドキュメントを参照してください。
上昇
昇順の並べ替えを指定するには、 Sorts.ascending()静的ファクトリー メソッドを使用します。 並べ替えるフィールドの名前をSorts.ascending()に渡します。
次の例では、サンプル コレクション内のドキュメントをorderTotalフィールドの昇順でソートします。
val resultsFlow = collection.find() .sort(Sorts.ascending(Order::orderTotal.name)) resultsFlow.collect { println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes)
下降
降順の並べ替えを指定するには、 Sorts.descending()静的ファクトリー メソッドを使用します。 並べ替えるフィールドの名前をSorts.descending()に渡します。
次の例では、サンプル コレクション内のドキュメントをorderTotalフィールドで降順にソートします。
val resultsFlow = collection.find() .sort(Sorts.descending(Order::orderTotal.name)) resultsFlow.collect { println(it) }
Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels) Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin)
ソート条件の組み合わせ
並べ替え条件を結合するには、 Sorts.orderBy()静的ファクトリー メソッドを使用します。 このメソッドは、ソート条件の順序付きリストを含むオブジェクトを構築します。 ソートを実行する際、前のソート条件が同値になった場合、ソートはリスト内の次のソート条件を使用して順序を決定します。
次の例では、サンプルコレクション内のドキュメントをdateフィールドで降順でソートし、同点の場合はorderTotalフィールドで昇順にソートします。
val orderBySort = Sorts.orderBy( Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name) ) val results = collection.find().sort(orderBySort) results.collect {println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels)
テキストスコア
テキスト検索結果は、検索結果が検索stringとどの程度一致するかを示す値であるテキスト スコアで並べ替えることができます。 テキスト検索のテキスト スコアで並べ替えを指定するには、 Sorts.metaTextScore()静的ファクトリー メソッドを使用します。
Sorts.metaTextScore()メソッドを使用して並べ替え条件を指定する方法の詳細な例については、並べ替えガイドのテキスト検索セクションを参照してください。
詳細については、「 ソートクラスのAPIドキュメント 」を参照してください。