- Reference >
- Operators >
- Aggregation Pipeline Operators >
- String Aggregation Operators >
- $split (aggregation)
$split (aggregation)¶
On this page
New in version 3.4.
Definition¶
-
$split¶ Divides a string into an array of substrings based on a delimiter.
$splitremoves the delimiter and returns the resulting substrings as elements of an array. If the delimiter is not found in the string,$splitreturns the original string as the only element of an array.$splithas the following operator expression syntax:Field Type Description string expressionstring The string to be split. string expressioncan be any valid expression as long as it resolves to a string. For more information on expressions, see Expressions.delimiterstring The delimiter to use when splitting the string expression. delimitercan be any valid expression as long as it resolves to a string.
Behavior¶
The $split operator returns an array.
The <string expression> and <delimiter> inputs must both be
strings. Otherwise, the operation fails with an error.
| Example | Results |
|---|---|
{ $split: [ "June-15-2013", "-" ] } |
[ "June", "15", "2013" ] |
{ $split: [ "banana split", "a" ] } |
[ "b", "n", "n", " split" ] |
{ $split: [ "Hello World", " " ] } |
[ "Hello", "World" ] |
{ $split: [ "astronomical", "astro" ] } |
[ "", "nomical" ] |
{ $split: [ "pea green boat", "owl" ] } |
[ "pea green boat" ] |
{ $split: [ "headphone jack", 7 ] } |
"$split requires an expression that evaluates to a string as
a second argument, found: double" |
{ $split: [ "headphone jack", /jack/ ] } |
"$split requires an expression that evaluates to a string as
a second argument, found: regex" |
Example¶
A collection named deliveries contains the following documents:
The goal of following aggregation operation is to find the total quantity of deliveries for each state and sort the list in descending order. It has five pipeline stages:
- The
$projectstage produces documents with two fields,qty(integer) andcity_state(array). The$splitoperator creates an array of strings by splitting thecityfield, using a space (" ") as a delimiter. - The
$unwindstage creates a separate record for each element in thecity_statefield. - The
$matchstage uses a regular expression to filter out the city documents, leaving only those containing a state. - The
$groupstage groups all the states together and sums theqtyfield. - The
$sortstage sorts the results bytotal_qtyin descending order.
The operation returns the following results: