Kotlin BSON • Multiplatform abstraction for different BSON implementations • opensavvy.ktmongo.bson • BsonPath
BsonPath¶
sealed interface BsonPath
Access specific fields in arbitrary BSON documents using a JSONPath-like API.
To access fields of a BSON document, use select or at.
Why BSON paths?¶
Most of the time, users want to deserialize documents, which they can do with opensavvy.ktmongo.bson.read.
However, sometimes, we receive large BSON payloads but only care about a few fields (for example, an explain plan). Writing an entire DTO for such payloads is time-consuming and complex.
Deserializing only a few specific fields can be much faster than deserializing the entire payload, as BSON is designed to allow skipping unwanted fields.
We may also face a payload that is too dynamic to easily deserialize, or with so much nesting that accessing fields becomes boilerplate.
In these situations, it may be easier (and often, more performant) to only deserialize a few specific fields, which is what BsonPath is useful for.
Syntax¶
BsonPath["foo"] // Refer to the field 'foo': $.foo
BsonPath[0] // Refer to the item at index 0: $[0]
BsonPath["foo"][0] // Refer to the item at index 0 in the array named 'foo': $.foo[0]
Accessing data¶
Find the first value for a given BSON path using at:
Find all values for a given BSON path using select:
Inheritors¶
Types¶
PathOrSelector¶
sealed interface PathOrSelector : BsonPath, BsonPath.Selector
Root¶
The root of a BsonPath expression.
Selector¶
sealed interface Selector
Represents a unique selector in a multi-selector segment.
Properties¶
all¶
Points to all children of a document.
parent¶
The parent path of this path: the same path without the last segment.
Functions¶
any¶
open fun any(vararg selectors: BsonPath.Selector): BsonPath
Allows specifying multiple selectors.
findIn¶
@LowLevelApi
abstract fun findIn(reader: BsonValueReader): Sequence<BsonValueReader>
Applies the filters described by this path on the reader.
get¶
reversed¶
Iterates a BsonArray in the reversed order, starting from the end.
sliced¶
open fun sliced(range: IntProgression): BsonPath