Sorting
The official MongoDB Scala driver supports sorting using the Sorts helper. One can still use that approach if desired.
However, scala-bson-query defines sorting support which allows:
// Using untyped.sorting
{
import io.github.osxhacker.query.mongodb.untyped.sorting._
import shapeless._
val result = collection.sort (by (_.name.ascending)).collect ()
val equivalent = collection.sort (
byEach {
doc =>
doc.name.ascending ::
HNil
}
.collect ()
}
// Using typed.sorting
{
import io.github.osxhacker.query.mongodb.typed.sorting._
import shapeless._
case class Nested (rating : Double)
case class ExampleDocument (aProperty : String, another : Int, nested : Nested)
val result = collection.sort (
by[ExampleDocument] {
doc =>
doc (_.another).desc ::
doc (_.nested.rating).asc ::
HNil
}
)
}
Methods
Typed
For the purposes of the method API reference, assume the following code is in scope:
import io.github.osxhacker.query.sorting.typed._
- by, a method which requires a functor that takes a
model.SortFieldAccessand produces a non-emptyHListofSortFielddefinitions.
Untyped
For the purposes of the method API reference, assume the following code is in scope:
import io.github.osxhacker.query.sorting.untyped._
-
by, overloaded method accepting between 1 and 4 functions taking
sorting.Untypedinstance(s), each which produce aSortFieldinstance. -
byEach, a method which requires a functor that takes a
sorting.Untypedand produces a non-emptyHListofSortFielddefinitions.
Operators
For the purposes of the operator API reference, assume the following code is in scope:
import io.github.osxhacker.query.reactive.untyped.sorting._
- asc, ascending Indicates ascending sort order.
by (_.aProperty.ascending, _.anotherProperty.asc)
- desc, descending Indicates descending sort order.
by (_.aProperty.descending, _.anotherProperty.desc)
The source code for this page can be found here.