slice

abstract fun slice(count: Int)(source)

Limits the number of array elements after the push operation.

If count is positive, after adding the elements to the array, only the first count elements are kept, and all additional elements are discarded.

If count is negative, after adding the elements to the array, only the last count elements are kept, and prior elements are discarded.

If this operator is specified without an each operator, the size of the array is truncated but no elements are added.

If this function is called multiple times, only the latest value has an effect.

Example

class User(
val name: String,
val tokens: List<String>,
)

collection.updateOne(
filter = {
User::name eq "Bob"
},
update = {
User::tokens push {
each("123", "456")
slice(3) // Keep only the first 3 elements
}
}
)

External resources