SortOptionDsl¶
interface SortOptionDsl<Document : Any> : CompoundBsonNode, FieldDsl
DSL to describe a sort order.
This DSL declares the methods ascending and descending which describe in which order elements are sorted.
For example,
will sort users in alphabetical order of their name, and when users have the same name, will sort them in decreasing order of their age.
See WithSort.sort.
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
The context used to generate this expression.
field¶
Converts a Kotlin property into a Field.
Functions¶
accept¶
Adds a new node as a child of this one.
acceptAll¶
Adds any number of nodes into this one.
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.
descending¶
abstract fun descending(field: Field<Document, *>)
If two documents have a different value of field, the one with greater 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 {
descending(User::age)
ascending(User::name)
}
},
filter = {}
)
This will return users from the oldest to the youngest. If multiple users have the same age, they are returned in the alphabetical order of their name.
See also
SortOptionDsl.ascending: To return documents in ascending order.
open fun descending(field: KProperty1<Document, *>)
If two documents have a different value of field, the one with greater 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 {
descending(User::age)
ascending(User::name)
}
},
filter = {}
)
This will return users from the oldest to the youngest. If multiple users have the same age, they are returned in the alphabetical order of their name.
See also
SortOptionDsl.ascending: To return documents in ascending order.
div¶
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
Refers to child as a nested field of the current field.
freeze¶
@LowLevelApi
abstract override fun freeze()
Makes this expression immutable.
get¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
simplify¶
Returns a simplified (but equivalent) expression to the current expression.
toBson¶
Writes the result of simplifying to a new BSON document.
toString¶
JSON representation of this expression.
unsafe¶
Refers to a field child of the current field, with no compile-time safety.
open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, with no compile-time safety.
writeTo¶
@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)
Writes the result of simplifying this expression into writer.