get

@JvmName(name = "bsonField")
operator fun <Child> BsonDocument.Companion.get(field: String): Field<BsonDocument, Child>(source)

Refers to a root field of the document being analyzed.

Example

val users = database.collection<BsonDocument>("users")

users.find {
BsonDocument.get<Int>("age") gte 18
}

@JvmName(name = "bsonField")
operator fun <Root, Child> Field<Root, BsonDocument>.get(field: String): Field<Root, Child>(source)
@JvmName(name = "bsonField")
operator fun <Root, Child> KProperty1<Root, BsonDocument>.get(field: String): Field<Root, Child>(source)

Refers to a child field of a field of type BsonDocument.

Example

class User(
val _id: ObjectId,
val name: String,
val externalData: BsonDocument,
)

users.find {
User::externalData.get<Profile?>("profile") ne null
}

@JvmName(name = "bsonItem")
operator fun <Root, Child> Field<Root, BsonArray>.get(index: Int): Field<Root, Child>(source)
@JvmName(name = "bsonItem")
operator fun <Root, Child> KProperty1<Root, BsonArray>.get(index: Int): Field<Root, Child>(source)

Refers to a child item of a field of type BsonArray.

Example

class User(
val _id: ObjectId,
val name: String,
val externalData: BsonArray,
)

users.find {
User::externalData.get<Profile?>(0) ne null
}

open operator fun <Root, Type> Field<Root, Collection<Type>>.get(index: Int): Field<Root, Type>(source)

Refers to a specific item in an array, by its index.

Examples

class User(
val name: String,
val friends: List<Friend>,
)

class Friend(
val name: String,
)

// Refer to the first friend
println(User::friends[0])
// → 'friends.0'

// Refer to the third friend's name
println(User::friends[2] / Friend::name)
// → 'friends.2.name'

open operator fun <Root, Type> KProperty1<Root, Collection<Type>>.get(index: Int): Field<Root, Type>(source)

Refers to a specific item in an array, by its index.

Examples

class User(
val name: String,
val friends: List<Friend>,
)

class Friend(
val name: String,
)

// Refer to the first friend
println(User::friends[0])
// → 'friends.$0'

// Refer to the third friend's name
println(User::friends[2] / Friend::name)
// → 'friends.$2.name'

open operator fun <Root, Type> Field<Root, Map<String, Type>>.get(key: String): Field<Root, Type>(source)
open operator fun <Root, Type> KProperty1<Root, Map<String, Type>>.get(index: String): Field<Root, Type>(source)

Refers to a specific item in a map, by its name.

Examples

class User(
val name: String,
val friends: Map<String, Friend>,
)

class Friend(
val name: String,
)

// Refer to the friend Bob
println(User::friends["bob"])
// → 'friends.bob'

// Refer to Bob's name
println(User::friends["bob"] / Friend::name)
// → 'friends.bob.name'