Skip to content

Kotlin BSON • Multiplatform abstraction for different BSON implementationsopensavvy.ktmongo.bsonBsonPathsliced

sliced

open fun sliced(range: IntProgression): BsonPath

Points to the elements of a BsonArray at the indices selected by range.

If the node is not an array, nothing is returned.

To create an open-ended range, use the overload that accepts integers.

To reverse an array, see reversed.

Range normalization

When a range has a step, the IntProgression class can reduce the closing bound if the step does not reach it. For example, 1 .. 6 step 2 becomes 1 .. 5 step 2 because both ranges cover the values [1, 3, 5] and 6 could not have been included. This doesn't impact the outputs, but may impact the toString representation of this path.

Examples

BsonPath.sliced(1..<5)         // $[1:5]: Items at indices 1 (inclusive) to 5 (exclusive)
BsonPath.sliced(1..5]          // $[1:6]: Items at indices 1 (inclusive) to 5 (inclusive)
BsonPath.sliced(1..5 step 2)   // $[1:6:2]: Items at indices 1 (inclusive) to 5 (inclusive), skipping every other item
BsonPath.sliced(5 downTo 1)    // $[1:6:2]: Items at indices 1 (inclusive) to 5 (inclusive), in reverse order

See also

open fun sliced(start: Int? = null, end: Int? = null, step: Int = 1): BsonPath

Points to the elements of a BsonArray at the indices selected by start and end, with an optional step.

If the node is not an array, nothing is returned.

Elements can be iterated in the reversed order by having a start greater than the end and having a negative step.

If the smaller bound is null, it means "the first element of the array, inclusive". If the larger bound is null, it means "the last element of the array, inclusive".

Examples

[0, 1, 2, 3, 4, 5, 6]
  • BsonPath.sliced(1, 3) returns [1, 2]

  • BsonPath.sliced(start = 5) returns [5, 6]

  • BsonPath.sliced(1, 5, 2) returns [1, 3]

  • BsonPath.sliced(5, 1, -2) returns [5, 3]

  • BsonPath.sliced(step = -1) returns [6, 5, 4, 3, 2, 1, 0]

Parameters

start

The index at which the slice should start, inclusive.

end

The index at which the slice should end, exclusive.

step

How many elements should be skipped between found elements.

  • With step of 1, all elements are returned.

  • With step of 2, every other element is returned.

  • With step of 0, no elements are returned at all.

See also