push
Adds value at the end of the array.
Unlike addToSet, this operator always adds the value, even if it's already present in the array.
Example
class User(
val name: String,
val age: Int,
val scores: List<Int>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::scores push 100
}
)Content copied to clipboard
This will add 100 to the user's scores, even if it's already present.
External resources
abstract fun <V> Field<T, Collection<V>>.push(builder: UpdateQuery.PushBuilder<V>.() -> Unit, type: KType)(source)
infix inline fun <V> Field<T, Collection<V>>.push(noinline builder: UpdateQuery.PushBuilder<V>.() -> Unit)(source)
infix inline fun <V> KProperty1<T, Collection<V>>.push(noinline builder: UpdateQuery.PushBuilder<V>.() -> Unit)(source)
Adds values to the end of the array with advanced options.
This method allows using MongoDB's advanced $push operators like $each and $slice.
Example
class User(
val name: String,
val age: Int,
val tokens: List<String>,
)
collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens push {
each("123", "456")
slice(3)
}
}
)Content copied to clipboard
This will add "123" and "456" to the user's tokens and keep only the last 3 elements.