sort
Orders the elements of an array during a $push operation.
If this function is called multiple times, only the last value has an effect.
If this function is called without an each operator, the existing data is sorted but no elements are added.
Example
class Quiz(
val id: Int,
val score: Int,
)
class User(
val name: String,
val quizzes: List<Quiz>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::quizzes push {
each(Quiz(3, 8), Quiz(4, 7), Quiz(5, 6))
sort {
ascending(Quiz::score)
}
}
}
)Content copied to clipboard
To sort based on the natural order of the array elements themselves:
class User(
val name: String,
val scores: List<Int>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::scores push {
each(12, 34)
sort { ascending() }
}
}
)Content copied to clipboard