diff --git a/examples/src/test/kotlin/SortTest.kt b/examples/src/test/kotlin/SortTest.kt index 8ed1699d..1e131027 100644 --- a/examples/src/test/kotlin/SortTest.kt +++ b/examples/src/test/kotlin/SortTest.kt @@ -19,31 +19,32 @@ internal class SortTest { companion object { // :snippet-start: sort-data-model - data class FoodOrder( + data class Order( @BsonId val id: Int, - val letter: String, - val food: String + val date: String, + val orderTotal: Double, + val description: String, ) // :snippet-end: val config = getConfig() val client = MongoClient.create(config.connectionUri) - val database = client.getDatabase("cafe") - val collection = database.getCollection("food_order") + val database = client.getDatabase("testDB") + val collection = database.getCollection("orders") @BeforeAll @JvmStatic fun beforeAll() { runBlocking { - val foodOrders = listOf( - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(6, "c", "maple donut") + val orders = listOf( + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin") ) - collection.insertMany(foodOrders) + collection.insertMany(orders) } } @@ -60,39 +61,39 @@ internal class SortTest { @Test fun basicSortTest() = runBlocking { // :snippet-start: basic-sort - collection.find().sort(Sorts.ascending("_id")) + val resultsFlow = collection.find().sort(Sorts.ascending(Order::orderTotal.name)) // :snippet-end: // Junit test for the above code val filter = Filters.empty() val expected = listOf( - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(6, "c", "maple donut") - + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"), + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes") ) - assertEquals(expected, collection.find(filter).sort((Sorts.ascending("_id"))).toList() ) + + assertEquals(expected, resultsFlow.toList() ) } @Test fun aggregationSortTest() = runBlocking { // :snippet-start: aggregation-sort val resultsFlow = collection.aggregate(listOf( - Aggregates.sort(Sorts.ascending("_id")) + Aggregates.sort(Sorts.ascending(Order::orderTotal.name)) )) resultsFlow.collect { println(it) } // :snippet-end: // Junit test for the above code val expected = listOf( - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(6, "c", "maple donut") + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"), + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes") ) assertEquals(expected, resultsFlow.toList()) } @@ -101,18 +102,18 @@ internal class SortTest { fun ascendingSortTest() = runBlocking { // :snippet-start: ascending-sort val resultsFlow = collection.find() - .sort(Sorts.ascending("_id")) + .sort(Sorts.ascending(Order::orderTotal.name)) resultsFlow.collect { println(it) } // :snippet-end: // Junit test for the above code val expected = listOf( - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(6, "c", "maple donut") + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"), + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes") ) assertEquals(expected, resultsFlow.toList()) } @@ -121,41 +122,41 @@ internal class SortTest { fun descendingSortTest() = runBlocking { // :snippet-start: descending-sort val resultsFlow = collection.find() - .sort(Sorts.descending("_id")) + .sort(Sorts.descending(Order::orderTotal.name)) resultsFlow.collect { println(it) } // :snippet-end: // Junit test for the above code val expected = listOf( - FoodOrder(6, "c", "maple donut"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(1, "c", "coffee with milk") - ) + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin") + ) assertEquals(expected, resultsFlow.toList()) } @Test fun handleTiesTest() = runBlocking { // :snippet-start: handle-ties-1 - collection.find().sort(Sorts.ascending(FoodOrder::letter.name)) + collection.find().sort(Sorts.ascending(Order::date.name)) // :snippet-end: val results = // :snippet-start: handle-ties-2 - collection.find().sort(Sorts.ascending(FoodOrder::letter.name, "_id")) + collection.find().sort(Sorts.ascending(Order::date.name, Order::orderTotal.name)) // :snippet-end: // Junit test for the above code val filter = Filters.empty() val expected = listOf( - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(5, "a", "milk and cookies"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(6, "c", "maple donut") - ) + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake") + ) assertEquals(expected, results.toList() ) } @@ -163,7 +164,7 @@ internal class SortTest { fun combineSortTest() = runBlocking { // :snippet-start: combine-sort val orderBySort = Sorts.orderBy( - Sorts.descending(FoodOrder::letter.name), Sorts.ascending("_id") + Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name) ) val results = collection.find().sort(orderBySort) @@ -172,12 +173,12 @@ internal class SortTest { // Junit test for the above code val filter = Filters.empty() val expected = listOf( - FoodOrder(1, "c", "coffee with milk"), - FoodOrder(6, "c", "maple donut"), - FoodOrder(4, "b", "coffee with sugar"), - FoodOrder(2, "a", "donuts and coffee"), - FoodOrder(3, "a", "maple syrup"), - FoodOrder(5, "a", "milk and cookies") + Order(6, "2022-01-23", 10.99, "1 bagel, 1 orange juice, 1 muffin"), + Order(5, "2022-01-23", 60.31, "one large vanilla and chocolate cake"), + Order(4, "2022-01-15", 43.62, "2 chicken lunches and a diet coke"), + Order(3, "2022-01-11", 19.49, "1 dozen vanilla cupcakes"), + Order(2, "2022-01-11", 83.87, "two medium vanilla birthday cakes"), + Order(1, "2022-01-03", 17.86, "1/2 lb cream cheese and 1 dozen bagels"), ) assertEquals(expected, results.toList() ) } @@ -185,24 +186,23 @@ internal class SortTest { @Test fun textSearchTest() = runBlocking { // :snippet-start: food-order-score - data class FoodOrderScore( + data class OrderScore( @BsonId val id: Int, - val letter: String, - val food: String, - val score: Double + val description: String, + val score: Double ) // :snippet-end: // :snippet-start: text-search - collection.createIndex(Indexes.text(FoodOrderScore::food.name)) + collection.createIndex(Indexes.text(Order::description.name)) val metaTextScoreSort = Sorts.orderBy( - Sorts.metaTextScore(FoodOrderScore::score.name), + Sorts.metaTextScore(OrderScore::score.name), Sorts.descending("_id") ) - val metaTextScoreProj = Projections.metaTextScore(FoodOrderScore::score.name) - val searchTerm = "maple donut" + val metaTextScoreProj = Projections.metaTextScore(OrderScore::score.name) + val searchTerm = "vanilla" val searchQuery = Filters.text(searchTerm) - val results = collection.find(searchQuery) + val results = collection.find(searchQuery) .projection(metaTextScoreProj) .sort(metaTextScoreSort) @@ -210,10 +210,10 @@ internal class SortTest { // :snippet-end: // Junit test for the above code val expected = listOf( - FoodOrderScore(6, "c", "maple donut",1.5), - FoodOrderScore(3, "a", "maple syrup", 0.75), - FoodOrderScore(2, "a", "donuts and coffee", 0.75), - ) + OrderScore(3, "1 dozen vanilla cupcakes", 0.625), + OrderScore(5, "one large vanilla and chocolate cake", 0.6), + OrderScore(2, "two medium vanilla birthday cakes", 0.6) + ) assertEquals(expected, results.toList()) } } \ No newline at end of file diff --git a/source/examples/generated/SortTest.snippet.aggregation-sort.kt b/source/examples/generated/SortTest.snippet.aggregation-sort.kt index c5870e25..265f8e7d 100644 --- a/source/examples/generated/SortTest.snippet.aggregation-sort.kt +++ b/source/examples/generated/SortTest.snippet.aggregation-sort.kt @@ -1,5 +1,5 @@ val resultsFlow = collection.aggregate(listOf( - Aggregates.sort(Sorts.ascending("_id")) + Aggregates.sort(Sorts.ascending(Order::orderTotal.name)) )) resultsFlow.collect { println(it) } diff --git a/source/examples/generated/SortTest.snippet.ascending-sort.kt b/source/examples/generated/SortTest.snippet.ascending-sort.kt index 08d22111..1aced045 100644 --- a/source/examples/generated/SortTest.snippet.ascending-sort.kt +++ b/source/examples/generated/SortTest.snippet.ascending-sort.kt @@ -1,4 +1,4 @@ val resultsFlow = collection.find() - .sort(Sorts.ascending("_id")) + .sort(Sorts.ascending(Order::orderTotal.name)) resultsFlow.collect { println(it) } diff --git a/source/examples/generated/SortTest.snippet.basic-sort.kt b/source/examples/generated/SortTest.snippet.basic-sort.kt index 77c7b833..11506bd1 100644 --- a/source/examples/generated/SortTest.snippet.basic-sort.kt +++ b/source/examples/generated/SortTest.snippet.basic-sort.kt @@ -1 +1 @@ -collection.find().sort(Sorts.ascending("_id")) +val resultsFlow = collection.find().sort(Sorts.ascending(Order::orderTotal.name)) diff --git a/source/examples/generated/SortTest.snippet.combine-sort.kt b/source/examples/generated/SortTest.snippet.combine-sort.kt index e71656cc..8d3e20b1 100644 --- a/source/examples/generated/SortTest.snippet.combine-sort.kt +++ b/source/examples/generated/SortTest.snippet.combine-sort.kt @@ -1,5 +1,5 @@ val orderBySort = Sorts.orderBy( - Sorts.descending(FoodOrder::letter.name), Sorts.ascending("_id") + Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name) ) val results = collection.find().sort(orderBySort) diff --git a/source/examples/generated/SortTest.snippet.descending-sort.kt b/source/examples/generated/SortTest.snippet.descending-sort.kt index 763f47be..bee6bda2 100644 --- a/source/examples/generated/SortTest.snippet.descending-sort.kt +++ b/source/examples/generated/SortTest.snippet.descending-sort.kt @@ -1,4 +1,4 @@ val resultsFlow = collection.find() - .sort(Sorts.descending("_id")) + .sort(Sorts.descending(Order::orderTotal.name)) resultsFlow.collect { println(it) } diff --git a/source/examples/generated/SortTest.snippet.food-order-score.kt b/source/examples/generated/SortTest.snippet.food-order-score.kt index df014ead..eff84496 100644 --- a/source/examples/generated/SortTest.snippet.food-order-score.kt +++ b/source/examples/generated/SortTest.snippet.food-order-score.kt @@ -1,6 +1,5 @@ -data class FoodOrderScore( +data class OrderScore( @BsonId val id: Int, - val letter: String, - val food: String, - val score: Double + val description: String, + val score: Double ) diff --git a/source/examples/generated/SortTest.snippet.handle-ties-1.kt b/source/examples/generated/SortTest.snippet.handle-ties-1.kt index a8ac605c..e640f891 100644 --- a/source/examples/generated/SortTest.snippet.handle-ties-1.kt +++ b/source/examples/generated/SortTest.snippet.handle-ties-1.kt @@ -1 +1 @@ -collection.find().sort(Sorts.ascending(FoodOrder::letter.name)) +collection.find().sort(Sorts.ascending(Order::date.name)) diff --git a/source/examples/generated/SortTest.snippet.handle-ties-2.kt b/source/examples/generated/SortTest.snippet.handle-ties-2.kt index 3424cfdb..8feceac1 100644 --- a/source/examples/generated/SortTest.snippet.handle-ties-2.kt +++ b/source/examples/generated/SortTest.snippet.handle-ties-2.kt @@ -1 +1 @@ -collection.find().sort(Sorts.ascending(FoodOrder::letter.name, "_id")) +collection.find().sort(Sorts.ascending(Order::date.name, Order::orderTotal.name)) diff --git a/source/examples/generated/SortTest.snippet.sort-data-model.kt b/source/examples/generated/SortTest.snippet.sort-data-model.kt index 0da84a59..9a3f1d1b 100644 --- a/source/examples/generated/SortTest.snippet.sort-data-model.kt +++ b/source/examples/generated/SortTest.snippet.sort-data-model.kt @@ -1,5 +1,6 @@ -data class FoodOrder( +data class Order( @BsonId val id: Int, - val letter: String, - val food: String + val date: String, + val orderTotal: Double, + val description: String, ) diff --git a/source/examples/generated/SortTest.snippet.text-search.kt b/source/examples/generated/SortTest.snippet.text-search.kt index cc8cd0de..18938179 100644 --- a/source/examples/generated/SortTest.snippet.text-search.kt +++ b/source/examples/generated/SortTest.snippet.text-search.kt @@ -1,13 +1,13 @@ -collection.createIndex(Indexes.text(FoodOrderScore::food.name)) +collection.createIndex(Indexes.text(Order::description.name)) val metaTextScoreSort = Sorts.orderBy( - Sorts.metaTextScore(FoodOrderScore::score.name), + Sorts.metaTextScore(OrderScore::score.name), Sorts.descending("_id") ) -val metaTextScoreProj = Projections.metaTextScore(FoodOrderScore::score.name) -val searchTerm = "maple donut" +val metaTextScoreProj = Projections.metaTextScore(OrderScore::score.name) +val searchTerm = "vanilla" val searchQuery = Filters.text(searchTerm) -val results = collection.find(searchQuery) +val results = collection.find(searchQuery) .projection(metaTextScoreProj) .sort(metaTextScoreSort) diff --git a/source/fundamentals/builders.txt b/source/fundamentals/builders.txt index 26741705..4660b942 100644 --- a/source/fundamentals/builders.txt +++ b/source/fundamentals/builders.txt @@ -1,3 +1,5 @@ +.. _kotlin-builders-landing: + ======== Builders ======== diff --git a/source/fundamentals/builders/aggregates.txt b/source/fundamentals/builders/aggregates.txt index 8cb74070..8d7ff868 100644 --- a/source/fundamentals/builders/aggregates.txt +++ b/source/fundamentals/builders/aggregates.txt @@ -1,3 +1,5 @@ +.. _aggregates-builders: + =================== Aggregates Builders =================== @@ -8,8 +10,6 @@ Aggregates Builders :depth: 2 :class: singlecol -.. _aggregates-builders: - Overview -------- diff --git a/source/fundamentals/builders/filters.txt b/source/fundamentals/builders/filters.txt index 912231d8..aa585c0d 100644 --- a/source/fundamentals/builders/filters.txt +++ b/source/fundamentals/builders/filters.txt @@ -1,3 +1,5 @@ +.. _filters-builders: + ================ Filters Builders ================ @@ -8,8 +10,6 @@ Filters Builders :depth: 2 :class: singlecol -.. _filters-builders: - Overview -------- diff --git a/source/fundamentals/builders/indexes.txt b/source/fundamentals/builders/indexes.txt index 1c178693..425f62dd 100644 --- a/source/fundamentals/builders/indexes.txt +++ b/source/fundamentals/builders/indexes.txt @@ -1,3 +1,5 @@ +.. _indexes-builders: + ================ Indexes Builders ================ @@ -8,8 +10,6 @@ Indexes Builders :depth: 2 :class: singlecol -.. _indexes-builders: - Overview -------- diff --git a/source/fundamentals/builders/projections.txt b/source/fundamentals/builders/projections.txt index c331d3db..fa62ccac 100644 --- a/source/fundamentals/builders/projections.txt +++ b/source/fundamentals/builders/projections.txt @@ -1,3 +1,5 @@ +.. _projections-builders: + ==================== Projections Builders ==================== @@ -8,8 +10,6 @@ Projections Builders :depth: 2 :class: singlecol -.. _projections-builders: - Overview -------- diff --git a/source/fundamentals/builders/sort.txt b/source/fundamentals/builders/sort.txt index 05fd34e2..4fd07f02 100644 --- a/source/fundamentals/builders/sort.txt +++ b/source/fundamentals/builders/sort.txt @@ -1,3 +1,6 @@ +.. _kotlin-sorts-builders: +.. _sorts-builders: + ============== Sorts Builders ============== @@ -8,31 +11,28 @@ Sorts Builders :depth: 2 :class: singlecol -.. _sorts-builders: - Overview -------- In this guide, you can learn how to specify **sort criteria** for your -queries using **builders** in the MongoDB Kotlin driver. +queries using **builders** in the {+driver-long+}. Sort criteria are the rules MongoDB uses to sort your data. Some examples of sort criteria are: -* Smallest number to largest number -* Earliest time of day to latest time of day -* Alphabetical order by first name +- Smallest number to largest number +- Earliest time of day to latest time of day +- Alphabetical order by first name -Builders are classes provided by the MongoDB Kotlin driver that help you -construct :ref:`BSON ` objects. To learn more, see our :doc:`guide -on builders `. +Builders are classes provided by the {+driver-short+} that help you +construct :ref:`BSON ` objects. To learn more, see the +:ref:`builders ` guide. -You should read this guide if you would like to use builders to specify sort -criteria for your queries. +You should read this guide if you want to learn how to use builders to +specify sort criteria for your queries. -If you want to learn the fundamentals of sorting in the MongoDB Kotlin -driver, consider reading our -:doc:`guide on sorting `. +To learn the fundamentals of sorting in the {+driver-short+}, see the +:ref:`sorting ` guide. .. _sorts-builders-sort-sample: @@ -41,12 +41,12 @@ contains the following documents: .. code-block:: json - {"_id": 1, "letter": "c", "food": "coffee with milk"} - {"_id": 3, "letter": "a", "food": "maple syrup"} - {"_id": 4, "letter": "b", "food": "coffee with sugar"} - {"_id": 5, "letter": "a", "food": "milk and cookies"} - {"_id": 2, "letter": "a", "food": "donuts and coffee"} - {"_id": 6, "letter": "c", "food": "maple donut"} + { "_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" } This data is modeled with the following Kotlin data class: @@ -60,8 +60,9 @@ The ``Sorts`` class is a builder that provides static factory methods for all so criteria operators supported by MongoDB. These methods return a ``Bson`` object that you can pass to the ``sort()`` method of a ``FindFlow`` instance or to ``Aggregates.sort()``. -If you want to learn more about the ``Aggregates`` -class, see our :doc:`guide on the Aggregates builder `. + +To learn more about the ``Aggregates`` +class, see the :ref:`Aggregates builder ` guide. For more information about the classes and interfaces in this section, see the following API Documentation: @@ -75,12 +76,12 @@ Ascending --------- To specify an ascending sort, use the ``Sorts.ascending()`` static -factory method. Pass ``Sorts.ascending()`` -the name of the field you need to sort on. +factory method. Pass the name of the field you want to sort on to +``Sorts.ascending()``. The following example sorts the documents in the :ref:`sample collection ` by ascending order -on the ``_id`` field: +on the ``orderTotal`` field: .. io-code-block:: @@ -90,22 +91,22 @@ on the ``_id`` field: .. output:: :language: console - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=6, letter=c, food=maple donut) + 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) Descending ---------- To specify a descending sort, use the ``Sorts.descending()`` static factory -method. Pass ``Sorts.descending()`` the name of the field you need to sort on. +method. Pass the name of the field you want to sort on to ``Sorts.descending()``. The following example sorts the documents in the :ref:`sample collection ` in descending order -on the ``_id`` field: +on the ``orderTotal`` field: .. io-code-block:: @@ -115,25 +116,25 @@ on the ``_id`` field: .. output:: :language: console - FoodOrder(id=6, letter=c, food=maple donut) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=1, letter=c, food=coffee with milk) + 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) Combining Sort Criteria ----------------------- To combine sort criteria, use the ``Sorts.orderBy()`` static factory method. This method constructs an object containing an ordered list of sort -criteria. When performing the sort, if the leftmost sort criteria results in a +criteria. When performing the sort, if the previous sort criteria result in a tie, the sort uses the next sort criteria in the list to determine the order. The following example sorts the documents in the :ref:`sample collection ` in descending order -on the ``letter`` field, and in the event of a tie, ascending order on the -``_id`` field: +on the ``date`` field, and in the event of a tie, ascending order on the +``orderTotal`` field: .. io-code-block:: @@ -143,12 +144,12 @@ on the ``letter`` field, and in the event of a tie, ascending order on the .. output:: :language: console - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=6, letter=c, food=maple donut) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=5, letter=a, food=milk and cookies) + 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) Text Score ---------- @@ -156,9 +157,11 @@ Text Score You can sort text search results by their text score, a value that indicates how closely a search result matches your search string. To specify a sort by the text score of a text search, use the ``Sorts.metaTextScore()`` static factory -method. For a detailed example showing how to specify sort criteria using +method. + +For a detailed example showing how to specify sort criteria using the ``Sorts.metaTextScore()`` method, see -:ref:`the text search section of our sorting fundamentals guide `. +:ref:`the text search section ` of the sorting guide. For more information, see the `Sorts class <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Sorts.html>`__ diff --git a/source/fundamentals/crud/read-operations/sort.txt b/source/fundamentals/crud/read-operations/sort.txt index 08b46cc7..045d3596 100644 --- a/source/fundamentals/crud/read-operations/sort.txt +++ b/source/fundamentals/crud/read-operations/sort.txt @@ -13,23 +13,23 @@ Sort Results Overview -------- -In this guide, you can learn how to use the **sort** operation to order your +In this guide, you can learn how to use **sort** operations to order your results from read operations with the MongoDB Kotlin driver. The sort operation orders the documents returned from your query by your specified **sort criteria**. Sort criteria are the rules you pass to MongoDB that describe how you would like your data to be ordered. Some examples of sort criteria are: -* Smallest number to largest number -* Earliest time of day to latest time of day -* Alphabetical order by first name +- Smallest number to largest number +- Earliest time of day to latest time of day +- Alphabetical order by first name -You should read this guide if you would like to: +You should read this guide to learn how to perform the following +actions: -* Perform ascending sorts and descending sorts. -* Combine sort criteria. -* Sort on the text score of a - :manual:`text search `. +- Perform ascending sorts and descending sorts +- Combine sort criteria +- Sort on the text score of a :manual:`text search ` .. _sorts-crud-sort-sample: @@ -38,12 +38,12 @@ documents: .. code-block:: json - {"_id": 1, "letter": "c", "food": "coffee with milk"} - {"_id": 3, "letter": "a", "food": "maple syrup"} - {"_id": 4, "letter": "b", "food": "coffee with sugar"} - {"_id": 5, "letter": "a", "food": "milk and cookies"} - {"_id": 2, "letter": "a", "food": "donuts and coffee"} - {"_id": 6, "letter": "c", "food": "maple donut"} + { "_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" } This data is modeled with the following Kotlin data class: @@ -53,12 +53,14 @@ This data is modeled with the following Kotlin data class: Methods For Sorting ------------------- -You can sort results retrieved by a query and you can sort results -within an aggregation pipeline. To sort your query results, use the +You can sort results retrieved by a query, or you can sort results +within an aggregation pipeline. + +To sort your query results, use the ``sort()`` method of a ``FindFlow`` instance. To sort your results within an aggregation pipeline, use the ``Aggregates.sort()`` static factory method. Both of these methods receive objects that implement the ``Bson`` interface as -arguments. For more information, see our API Documentation for the +arguments. For more information, see the API Documentation for the `BSON interface <{+api+}/apidocs/bson/org/bson/conversions/Bson.html>`__. You can use the ``sort()`` method of a ``FindFlow`` instance as follows: @@ -69,7 +71,7 @@ You can use the ``sort()`` method of a ``FindFlow`` instance as follows: You can use the ``Aggregates.sort()`` method within an aggregation pipeline to sort the documents in the :ref:`sample collection ` from smallest to -largest value of the ``_id`` field as follows: +largest value of the ``orderTotal`` field as follows: .. io-code-block:: @@ -79,19 +81,18 @@ largest value of the ``_id`` field as follows: .. output:: :language: console - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=6, letter=c, food=maple donut) + 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) -In the preceding code snippets, we specify our sort criteria using the ``Sorts`` +In the preceding code snippets, we specify the sort criteria using the ``Sorts`` builder class. While it is possible to specify sort criteria using any class that implements the ``Bson`` interface, we recommend that you specify sort criteria through the ``Sorts`` builder. For more information on the ``Sorts`` -builder class, see our -:doc:`guide on the Sorts builder `. +builder class, see the :ref:`Sorts builder ` guide. For more information about the classes and interfaces in this section, see the following API Documentation: @@ -111,15 +112,15 @@ descending sort orders your results from largest to smallest. Here are some examples of data sorted in ascending order: -* Numbers: 1, 2, 3, 43, 43, 55, 120 -* Dates: 1990-03-10, 1995-01-01, 2005-10-30, 2005-12-21 -* Words (ASCII): Banana, Dill, carrot, cucumber, hummus +- Numbers: 1, 2, 3, 43, 43, 55, 120 +- Dates: 1990-03-10, 1995-01-01, 2005-10-30, 2005-12-21 +- Words (ASCII): Banana, Dill, carrot, cucumber, hummus Here are some examples of data sorted in descending order: -* Numbers: 100, 30, 12, 12, 9, 3, 1 -* Dates: 2020-01-01, 1998-12-11, 1998-12-10, 1975-07-22 -* Words (reverse ASCII): pear, grapes, apple, Cheese +- Numbers: 100, 30, 12, 12, 9, 3, 1 +- Dates: 2020-01-01, 1998-12-11, 1998-12-10, 1975-07-22 +- Words (reverse ASCII): pear, grapes, apple, Cheese The following subsections show how to specify these sort criteria. @@ -143,7 +144,7 @@ specified field name. In the following code example, we use the ``ascending()`` method to sort the :ref:`sample collection ` -by the ``_id`` field: +by the ``orderTotal`` field: .. io-code-block:: @@ -153,12 +154,12 @@ by the ``_id`` field: .. output:: :language: console - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=6, letter=c, food=maple donut) + 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) Descending ~~~~~~~~~~ @@ -167,9 +168,9 @@ To specify a descending sort, use the ``Sorts.descending()`` static factory method. Pass the ``Sorts.descending()`` method the name of the field you need to sort in descending order. The following code snippet shows how to specify a descending sort on the -``_id`` field and return the documents in the +``orderTotal`` field and return the documents in the :ref:`sample collection ` -in descending order: +in descending order: .. io-code-block:: @@ -179,12 +180,12 @@ in descending order: .. output:: :language: console - FoodOrder(id=6, letter=c, food=maple donut) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=1, letter=c, food=coffee with milk) + 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-crud-handling-ties: @@ -197,28 +198,18 @@ the event of ties. For example, suppose we encounter a tie when applying a sort to the :ref:`sample collection ` using the following code: -.. io-code-block:: - - .. input:: /examples/generated/SortTest.snippet.handle-ties-1.kt - :language: kotlin - - .. output:: - :language: console - - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=2, letter=a, food=donuts and coffee) +.. literalinclude:: /examples/generated/SortTest.snippet.handle-ties-1.kt + :language: kotlin -Since multiple documents that matched our query contain the value "a" for the -field on which we perform the sort, the documents could be returned -in any order. +Since multiple documents that matched the query contain the same value +in the ``date`` field, the documents may not be returned in a consistent order. -If you need to guarantee a specific sort order for documents that have fields +If you need to guarantee a specific order for documents that have fields with identical values, you can specify additional fields to sort on in the event of a tie. -We can specify an ascending sort on the ``letter`` field followed by the -``_id`` field return the documents in the +We can specify an ascending sort on the ``date`` field followed by the +``orderTotal`` field to return the documents in the :ref:`sample collection ` in the following order: @@ -230,26 +221,26 @@ in the following order: .. output:: :language: console - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=5, letter=a, food=milk and cookies) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=6, letter=c, food=maple donut) + 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=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes) + Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) + 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) Combining Sort Criteria ~~~~~~~~~~~~~~~~~~~~~~~ To combine sort criteria, use the ``Sorts.orderBy()`` static factory method. This method constructs an object containing an ordered list of sort -criteria. When performing the sort, if the leftmost sort criteria results in a +criteria. When performing the sort, if the previous sort criteria result in a tie, the sort uses the next sort criteria in the list to determine the order. -In the following code snippet, we use the ``orderBy()`` method to order our data -by performing a descending sort on the ``letter`` field, and in the event of a -tie, by performing an ascending sort on the ``_id`` field, then returns the -documents in the :ref:`sample collection ` -in the following order: +In the following code snippet, we use the ``orderBy()`` method to order the data +by performing a descending sort on the ``date`` field, and in the event of a +tie, by performing an ascending sort on the ``orderTotal`` field. With +these sort criteria, the code returns the documents in the :ref:`sample +collection ` in the following order: .. io-code-block:: @@ -259,12 +250,12 @@ in the following order: .. output:: :language: console - FoodOrder(id=1, letter=c, food=coffee with milk) - FoodOrder(id=6, letter=c, food=maple donut) - FoodOrder(id=4, letter=b, food=coffee with sugar) - FoodOrder(id=2, letter=a, food=donuts and coffee) - FoodOrder(id=3, letter=a, food=maple syrup) - FoodOrder(id=5, letter=a, food=milk and cookies) + 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) .. _sorts-crud-text-search: @@ -290,17 +281,17 @@ sort by the text score. In the following code example, we show how you can use the ``Sorts.metaTextScore()`` method to sort the results of a text search on the :ref:`sample collection `. -The code example uses the :doc:`Filters `, -:doc:`Indexes `, and -:doc:`Projections ` builders. +The code example uses the :ref:`Filters `, +:ref:`Indexes `, and +:ref:`Projections ` builders. The code example performs the following actions: #. Creates a text index for your :ref:`sample collection ` - on the ``food`` field. If you call ``createIndex()`` specifying an index that + on the ``description`` field. If you call ``createIndex()`` specifying an index that already exists on the collection, the operation does not create a new index. -#. Runs your text search for the phrase "maple donut". +#. Runs your text search for the phrase ``"vanilla"``. #. Projects text scores into your query results as the ``score`` field. #. Sorts your results by text score (best match first). @@ -325,9 +316,9 @@ The data is modeled with the following Kotlin data class: .. output:: :language: console - FoodOrderScore(id=6, letter=c, food=maple donut, score=1.5) - FoodOrderScore(id=3, letter=a, food=maple syrup, score=0.75) - FoodOrderScore(id=2, letter=a, food=donuts and coffee, score=0.75) + OrderScore(id=3, description=1 dozen vanilla cupcakes, score=0.625) + OrderScore(id=5, description=one large vanilla and chocolate cake, score=0.6) + OrderScore(id=2, description=two medium vanilla birthday cakes, score=0.6) .. note:: Text Search Behavior in MongoDB 4.4 or Later