BsonPath

fun BsonPath(@Language(value = "JSONPath") text: String): BsonPath(source)

Parses an RFC-9535 compliant string expression into a BsonPath instance.

This function is the mirror of the BsonPath.toString methods.

Warning. Not everything from the RFC is implemented at the moment. As a rule of thumb, if text can be returned by the BsonPath.toString function of a segment, then it can be parsed by this function.

SyntaxDescription
$The root identifier. See BsonPath.Root.
['foo'] or .fooAccessor for a field named foo. See BsonPath.get.
[0]Accessor for the first item of an array. See BsonPath.get.
.* or [*]Accessor for all direct children. See BsonPath.all.
[1:3]Accessor for elements at index 1..<3. See BsonPath.sliced.
[?@.a > @.b]Accessor for elements that satisfy the given condition.

Multiple selectors can be defined in the same brackets. When this is the case, all nodes that match any of the selectors are returned. For example, ['foo', 'bar', 'baz'] will return the values of the fields foo, bar and baz. See BsonPath.any.

Examples

val document: Bson = …

val id: Uuid = document at BsonPath("$.profile.id")

for (user in document.select<User>(BsonPath("$.friends"))) {
println("User: $user")
}

See also

Learn more about BSON paths.

Access one field by its BSON path.

Access multiple fields by their BSON path.