Path¶
@LowLevelApi
data class Path(val segment: PathSegment, val parent: Path?) : Comparable<Path>
Low-level, type-unsafe pointer to a specific field in a document.
A path is a string pointer that identifies which field(s) are impacted by an operator.
For example, the following are valid paths:
-
"foo": targets the field "foo", -
"foo.bar": targets the field "bar" which is part of the object "foo", -
"arr.5.bar": targets the field "bar" which is part of the item with index 5 in the array "arr".
This structure is a singly-linked list representing the entire path. Each segment is represented by PathSegment.
The high-level type-safe equivalent of this type is Field.
Constructors¶
Path¶
constructor(segment: PathSegment, parent: Path?)
Path¶
@LowLevelApi
fun Path(root: String): Path
Types¶
Companion¶
object Companion
Properties¶
parent¶
The previous link in this Path.
For example, the path Path(PathSegment.Indexed(5), Path(PathSegment.Field("foo"), null)) represents the path "foo.5".
segment¶
val segment: PathSegment
Functions¶
asSequence¶
@LowLevelApi
fun asSequence(): Sequence<PathSegment>
Returns a Sequence of the different PathSegment instances that form this Path, in hierarchical order.
For example, the path Path(PathSegment.Indexed(5), Path(PathSegment.Field("foo"), null)) represents the path "foo.5" and would return the sequence [PathSegment.Field("foo"), PathSegment.Indexed(5)].
compareTo¶
Compares this path with another path.
The paths are compared based on order when walking a filesystem:
-
The paths are compared by their length, with shorter paths being considered "less than" longer paths.
-
If the paths have the same length, they are compared by their segments, in order. The comparison is done by comparing the segment types first, and then the segment values (in alphanumérical order).
Therefore, this is a possible order:
div¶
@LowLevelApi
operator fun Path.div(segment: PathSegment): Path
@LowLevelApi
@DangerousMongoApi
operator fun Path.div(path: Path): Path
toBsonPath¶
@LowLevelApi
fun Path.toBsonPath(): BsonPath
fun Field<*, *>.toBsonPath(): BsonPath
Converts this MongoDB Field to a BsonPath.
BsonPath is an implementation of RFC9535 JSONPath. See its documentation for more information.
BsonPath can be useful to access elements from an arbitrary BSON document, for example, after a projection.
Example
data class User(
val _id: ObjectId,
val profile: Profile,
)
data class Profile(
val name: String,
)
val fieldName = (User::profile / Profile::name).toBsonPath()
val bson: Bson = …
val name = bson at fieldName
See also
-
select: Select multiple values. -
selectFirst: Select the first value. -
at: Select the first value, as an infix operator.