Skip to content

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

selectFirst

inline fun <T> Bson.selectFirst(path: BsonPath): T

Finds the first value that matches path in a given BSON document.

Example

val document: Bson = 

document.selectFirst<String>(BsonPath["foo"]["bar"])

will return the value of the field foo.bar.

Alternatives

Depending on your situation, you can also use the equivalent function at:

val document: Bson = 

val bar: String = document at BsonPath["foo"]["bar"]

See also

  • BsonPath Learn more about BSON paths.

  • select Select multiple values with a BSON path.

Throws

NoSuchElementException

If no element is found matching the path.

inline fun <T> Bson.selectFirst(@Language
(value = "JSONPath") path: String): T

Finds the first value that matches path in a given BSON document.

To learn more about the syntax, see BsonPath.

Example

val document: Bson = 

document.selectFirst<String>("$.foo.bar")

will return the value of the field foo.bar.

See also

  • BsonPath Learn more about BSON paths.

  • select Select multiple values with a BSON path.

  • at Select a single value using infix notation.