Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.aggregation.operatorsStringValueOperatorssplit

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

open fun <Context : Any> Value<Context, String>.split(delimiter: String): Value<Context, List<String>?>

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