The $split operator breaks a string into an array of substrings using a delimiter. Use it to parse comma-separated tags, email addresses, file paths, and other structured text stored as a single field.
01
String → Array
Split by delimiter.
02
Syntax
[ string, delimiter ].
03
Parse CSV-like
Comma, space, @.
04
$project Stage
New array fields.
05
Use Cases
Tags, emails, paths.
06
vs $concat
Split vs join.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $split operator divides a string wherever a delimiter appears and returns an array of substrings. For example, { $split: [ "a,b,c", "," ] } returns ["a", "b", "c"].
This is the opposite of $concat, which joins strings together. After splitting, you can use $arrayElemAt, $size, or $sortArray on the resulting array.
💡
Beginner Tip
If the delimiter is not found, MongoDB returns an array with the whole string as one element. Empty trailing segments are preserved: "a,b," split on "," gives ["a", "b", ""].
Foundation
📝 Syntax
The $split operator takes a string and a delimiter:
$split produces an array; $size counts its elements. Useful for validation and reporting.
Applications
🚀 Use Cases
CSV import cleanup — convert legacy comma-separated fields into arrays for querying.
Email parsing — extract username or domain from an address string.
Path and URL parsing — split file paths or URL segments for analysis.
Tokenization — break delimited text before sorting, filtering, or set operations.
🧠 How $split Works
1
MongoDB reads the string
The first argument resolves to a string (field path, literal, or nested expression).
Input
2
The delimiter is applied
MongoDB finds every occurrence of the delimiter and cuts the string at those positions.
Split
3
Substrings become an array
Each piece between delimiters becomes an array element. Empty segments are kept.
Output
=
✂
String array
Use with $arrayElemAt, $size, or $sortArray.
Wrap Up
Conclusion
The $split operator converts delimited strings into arrays inside aggregation pipelines. It is essential for parsing legacy CSV fields, email addresses, and paths before further array processing.
Chain it with $arrayElemAt to pick one part, or $size to count segments. Next in the series: $sqrt.
Use $split to normalize CSV-like string fields into arrays
Combine with $arrayElemAt when you need a single segment
Use $trim on segments if values may have extra whitespace
Guard null strings with $ifNull: [ "$field", "" ]
Count parsed items with $size for validation
❌ Don’t
Confuse $split (string → array) with $concat (strings → string)
Expect regex splitting — $split uses literal delimiter strings only
Forget that no delimiter match returns a one-element array
Assume trimmed values — spaces around commas stay in the result
Use empty string as delimiter — it causes an error
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $split
Use these points when parsing strings in MongoDB pipelines.
5
Core concepts
✂01
String → Array
By delimiter.
Purpose
📝02
[ str, delim ]
Two arguments.
Syntax
🛠03
No match
One-element array.
Rule
🗃04
+ $arrayElemAt
Pick one part.
Chaining
⚠05
vs $concat
Split vs join.
Compare
❓ Frequently Asked Questions
$split divides a string into an array of substrings using a delimiter. For example, { $split: [ "a,b,c", "," ] } returns ["a", "b", "c"]. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $split: [ <string expression>, <delimiter> ] }. The first argument is the string to split; the second is the delimiter string. Both can be field references or literals.
MongoDB returns an array containing the entire original string as a single element. For example, $split on "hello" with delimiter "," returns ["hello"].
$split breaks one string into an array using a delimiter. $concat joins multiple strings into one string. They are inverse operations for simple delimiter-based parsing.
If the string expression is null, $split returns null. Use $ifNull to provide a default empty string when the field may be missing.