StringValueOperators¶
interface StringValueOperators : ValueOperators
String aggregation operators.
External resources¶
Inheritors¶
Properties¶
context¶
@LowLevelApi
abstract override val context: BsonContext
The strategy used when converting from KProperty1 to Field.
field¶
Converts a Kotlin property into a Field.
length¶
Returns the number of code points in the specified string.
If the argument resolves to null, this function returns null.
Counting characters¶
This function uses MongoDB's $strLenCP operator, which counts characters using Unicode code points. This differs from Kotlin's String.length, which uses UTF-16 code units. For strings containing characters outside the Basic Multilingual Plane (like emoji or certain mathematical symbols), the counting behavior will differ.
For example, the emoji "👨👩👧👦" (family) is a single Unicode grapheme cluster but consists of multiple code points. According to this operator, it has a length of 7. However, according to Kotlin's String.length, it has a length of 11.
Example¶
class Document(
val text: String,
val length: Int,
)
collection.aggregate()
.set {
Document::length set of(Document::text).length
}.toList()
External resources¶
See also
lengthUTF8¶
Returns the number of UTF-8 encoded bytes in the specified string.
If the argument resolves to null, this function returns null.
Counting characters¶
This function uses MongoDB's $strLenBytes operator, which counts characters using UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode. This differs from the length property which uses Unicode code points.
For example, US-ASCII characters are encoded using one byte. Characters with diacritic markings and additional Latin alphabetical characters are encoded using two bytes. Chinese, Japanese and Korean characters typically require three bytes, and other planes of Unicode (emoji, mathematical symbols, etc.) require four bytes.
Example¶
class Document(
val text: String,
val byteLength: Int,
)
collection.aggregate()
.set {
Document::byteLength set of(Document::text).lengthUTF8
}.toList()
External resources¶
See also
Functions¶
concat¶
Concatenates strings together.
If any of strings are null, the concatenation returns null.
Example¶
class Document(
val firstName: String,
val lastName: String,
val fullName: String,
)
collection.aggregate()
.set {
Document::fullName set concat(of(Document::firstName), of(" "), of(Document::lastName))
}
.toList()
External resources¶
Concatenates strings together.
If any of strings are null, the concatenation returns null.
Example¶
class Document(
val firstName: String,
val lastName: String,
val fullName: String,
)
collection.aggregate()
.set {
Document::fullName set concat(of(Document::firstName), of(" "), of(Document::lastName))
}
.toList()
External resources¶
Concatenates strings together.
If any of strings are null, the concatenation returns null.
Example¶
class Document(
val firstName: String,
val lastName: String,
val fullName: String,
)
collection.aggregate()
.set {
Document::fullName set (of(Document::firstName) concat of(" ") concat of(Document::lastName))
}
.toList()
External resources¶
div¶
Refers to field as a nested field of the current value.
Examples¶
class User(
val name: String,
)
class Data(
val users: List<User>,
val userNames: List<Int>,
)
data.aggregate()
.set {
Data::userNames set Data::users.map { it / User::name }
}
External resources¶
Refers to field as a nested field of the current value.
Examples¶
class User(
val name: String,
)
class Data(
val users: List<User>,
val userNames: List<Int>,
)
data.aggregate()
.set {
Data::userNames set Data::users.map { it / User::name }
}
External resources¶
get¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
lowercase¶
Converts a string to lowercase, returning the result.
If the argument resolves to null, $toLower returns an empty string "".
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).lowercase()
}.toList()
External resources¶
of¶
Refers to a field within an aggregation value.
Example¶
Refers to a field within an aggregation value.
Example¶
Refers to a Kotlin value within an aggregation value.
Example¶
Refers to a BsonType within an aggregation value.
Example¶
replace¶
Replaces all instances of find with a replacement string.
If no occurrences of find are found in the input string, the input string is returned. If any of the input, find or replacement is null, null is returned.
The input, find, and replacement expressions must evaluate to a string or a null, or $replaceAll fails with an error.
Example¶
class Document(
val item: String,
)
collection.aggregate()
.set {
Document::item set of(Document::item).replace(find = of("blue paint"), replacement = of("red paint"))
}.toList()
External resources¶
Replaces all instances of find with a replacement string.
If no occurrences of find are found in the input string, the input string is returned. If the input is null, null is returned.
The input must evaluate to a string or a null, or $replaceAll fails with an error.
Example¶
class Document(
val item: String,
)
collection.aggregate()
.set {
Document::item set of(Document::item).replace(find = "blue paint", replacement = "red paint")
}.toList()
External resources¶
replaceFirst¶
Replaces the first instance of find with a replacement string.
If no occurrences of find are found in the input string, the input string is returned. If any of the input, find or replacement is null, null is returned.
The input, find, and replacement expressions must evaluate to a string or a null, or $replaceOne fails with an error.
Example¶
class Document(
val item: String,
)
collection.aggregate()
.set {
Document::item set of(Document::item).replaceFirst(find = of("blue paint"), replacement = of("red paint"))
}.toList()
External resources¶
Replaces the first instance of find with a replacement string.
If no occurrences of find are found in the input string, the input string is returned. If the input is null, null is returned.
The input must evaluate to a string or a null, or $replaceOne fails with an error.
Example¶
class Document(
val item: String,
)
collection.aggregate()
.set {
Document::item set of(Document::item).replaceFirst(find = "blue paint", replacement = "red paint")
}.toList()
External resources¶
split¶
Divides a string into an array of substrings based on a delimiter.
$split removes the delimiter and returns the resulting substrings as elements of an array. If the delimiter is not found in the string, $split returns the original string as the only element of an array.
Both the string expression and delimiter must be strings. Otherwise, the operation fails with an error.
Example¶
class Document(
val city: String,
val cityState: List<String>,
)
collection.aggregate()
.set {
Document::cityState set of(Document::city).split(of(", "))
}.toList()
External resources¶
Divides a string into an array of substrings based on a delimiter.
$split removes the delimiter and returns the resulting substrings as elements of an array. If the delimiter is not found in the string, $split returns the original string as the only element of an array.
The string expression must be a string. Otherwise, the operation fails with an error.
Example¶
class Document(
val city: String,
val cityState: List<String>,
)
collection.aggregate()
.set {
Document::cityState set of(Document::city).split(", ")
}.toList()
External resources¶
substring¶
Returns the substring of a string.
The substring starts with the character at the specified Unicode code point startIndex (zero-based) in the string and continues for the length number of code points specified.
Note that this behavior is different from String.substring, which expects start and end indexes.
Counting characters¶
This function uses MongoDB's $substrCP operator, which counts characters using Unicode code points. This differs from Kotlin's String.substring, which uses UTF-16 code units. For strings containing characters outside the Basic Multilingual Plane (like emoji or certain mathematical symbols), the indexing behavior will differ.
For example, the emoji "👨👩👧👦" (family) is a single Unicode grapheme cluster but consists of multiple code points. According to this operator, it has a size of 5. However, according to Kotlin's String.substring, it has a size of 11.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).substring(startIndex = of(1), length = of(2))
}.toList()
External resources¶
See also
Returns the substring of a string.
The substring contains the Unicode code points that are contained within indexes.
Counting characters¶
This function uses MongoDB's $substrCP operator, which counts characters using Unicode code points. This differs from Kotlin's String.substring, which uses UTF-16 code units. For strings containing characters outside the Basic Multilingual Plane (like emoji or certain mathematical symbols), the indexing behavior will differ.
For example, the emoji "👨👩👧👦" (family) is a single Unicode grapheme cluster but consists of multiple code points. According to this operator, it has a size of 5. However, according to Kotlin's String.substring, it has a size of 11.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).substring(1..2)
}.toList()
External resources¶
See also
substringUTF8¶
Returns the substring of a string.
The substring starts with the character at the specified UTF-8 byte startIndex in the string and continues for the byteCount number of bytes.
Note that this behavior is different from String.substring, which expects start and end indexes.
Counting characters¶
This function uses MongoDB's $substrBytes operator, which counts characters using UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode. This differs from the substring function which uses Unicode code points.
For example, US-ASCII characters are encoded using one byte. Characters with diacritic markings and additional Latin alphabetical characters are encoded using two bytes. Chinese, Japanese and Korean characters typically require three bytes, and other planes of Unicode (emoji, mathematical symbols, etc.) require four bytes.
If startIndex or byteCount happen to be within a multibyte character, an error will be thrown.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).substringUTF8(startIndex = of(1), byteCount = of(2))
}.toList()
External resources¶
See also
Returns the substring of a string.
The substring contains the Unicode code points that are contained within indexes.
Counting characters¶
This function uses MongoDB's $substrBytes operator, which counts characters using UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode. This differs from the substring function which uses Unicode code points.
For example, US-ASCII characters are encoded using one byte. Characters with diacritic markings and additional Latin alphabetical characters are encoded using two bytes. Chinese, Japanese and Korean characters typically require three bytes, and other planes of Unicode (emoji, mathematical symbols, etc.) require four bytes.
If the start or end index happens to be within a multibyte character, an error will be thrown.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).substringUTF8(1..2)
}.toList()
External resources¶
See also
trim¶
Removes whitespace characters, including null, or the specified characters from the beginning and end of a string.
By default, removes whitespace characters including the null character.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).trim()
}.toList()
External resources¶
Removes the specified characters from the beginning and end of a string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the beginning and end
collection.aggregate()
.set {
Document::text set of(Document::text).trim('g', 'e')
}.toList()
// Trim space, 'g', and 'e' characters from the beginning and end
collection.aggregate()
.set {
Document::text set of(Document::text).trim(' ', 'g', 'e')
}.toList()
External resources¶
Removes the specified characters from the beginning and end of a string.
The characters parameter is a single string that can contain multiple characters to be trimmed. Each character in the string will be removed from both the beginning and end of the input string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the beginning and end
collection.aggregate()
.set {
Document::text set of(Document::text).trim(chars = of("ge"))
}.toList()
// Trim space, 'g', and 'e' characters from the beginning and end
collection.aggregate()
.set {
Document::text set of(Document::text).trim(chars = of(" ge"))
}.toList()
External resources¶
trimEnd¶
Removes whitespace characters, including null, or the specified characters from the end of a string.
By default, removes whitespace characters including the null character.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).trimEnd()
}.toList()
External resources¶
Removes the specified characters from the end of a string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the end
collection.aggregate()
.set {
Document::text set of(Document::text).trimEnd('g', 'e')
}.toList()
// Trim space, 'g', and 'e' characters from the end
collection.aggregate()
.set {
Document::text set of(Document::text).trimEnd(' ', 'g', 'e')
}.toList()
External resources¶
Removes the specified characters from the end of a string.
The characters parameter is a single string that can contain multiple characters to be trimmed. Each character in the string will be removed from the end of the input string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the end
collection.aggregate()
.set {
Document::text set of(Document::text).trimEnd(characters = of("ge"))
}.toList()
// Trim space, 'g', and 'e' characters from the end
collection.aggregate()
.set {
Document::text set of(Document::text).trimEnd(characters = of(" ge"))
}.toList()
External resources¶
trimStart¶
Removes whitespace characters, including null, or the specified characters from the beginning of a string.
By default, removes whitespace characters including the null character.
Example¶
class Document(
val text: String,
)
collection.aggregate()
.set {
Document::text set of(Document::text).trimStart()
}.toList()
External resources¶
Removes the specified characters from the beginning of a string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the beginning
collection.aggregate()
.set {
Document::text set of(Document::text).trimStart('g', 'e')
}.toList()
// Trim space, 'g', and 'e' characters from the beginning
collection.aggregate()
.set {
Document::text set of(Document::text).trimStart(' ', 'g', 'e')
}.toList()
External resources¶
Removes the specified characters from the beginning of a string.
The characters parameter is a single string that can contain multiple characters to be trimmed. Each character in the string will be removed from the beginning of the input string.
Example¶
class Document(
val text: String,
)
// Trim both 'g' and 'e' characters from the beginning
collection.aggregate()
.set {
Document::text set of(Document::text).trimStart(characters = of("ge"))
}.toList()
// Trim space, 'g', and 'e' characters from the beginning
collection.aggregate()
.set {
Document::text set of(Document::text).trimStart(characters = of(" ge"))
}.toList()
External resources¶
unsafe¶
Refers to a field child of the current field, with no compile-time safety.
open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, without checking that it is a field available on the current object.
Refers to a field child of the current field, with no compile-time safety.