MongoDB request DSL • opensavvy.ktmongo.dsl.options • SortOptionDsl • ascending
ascending¶
If two documents have a different value of field
, the one with lesser value will be returned first.
If this function is called multiple times, the first call takes precedence: subsequent calls determine the order of documents when they are equal according to the first call.
Example¶
class User(
val name: String,
val age: Int,
)
users.find(
options = {
sort {
ascending(User::age)
ascending(User::name)
}
},
filter = {}
)
This will return users from the youngest to the oldest. If multiple users have the same age, they are returned in the alphabetical order of their name.
See also¶
SortOptionDsl.descending
To return documents in descending order.
open fun ascending(field: KProperty1<Document, *>)
If two documents have a different value of field
, the one with lesser value will be returned first.
If this function is called multiple times, the first call takes precedence: subsequent calls determine the order of documents when they are equal according to the first call.
Example¶
class User(
val name: String,
val age: Int,
)
users.find(
options = {
sort {
ascending(User::age)
ascending(User::name)
}
},
filter = {}
)
This will return users from the youngest to the oldest. If multiple users have the same age, they are returned in the alphabetical order of their name.
See also¶
SortOptionDsl.descending
To return documents in descending order.