$split is a MongoDB aggregation expression that divides a string into an array of substrings using a delimiter. The delimiter is removed. If it is not found, you get a one-element array with the original string.
Non-empty separator string (or regex on newer versions)
$arrayElemAt
No
Take one segment by index
Pseudocode
Pseudocode
for each document:
s = evaluate(stringExpression)
d = evaluate(delimiter)
if s is null or missing:
result = null
else if s is not a string or d is empty / invalid:
error // aggregation fails
else if d not found in s:
result = [s]
else:
result = split(s, d) // array of pieces
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing string-split expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $split: [ <string>, <delimiter> ] }
Return type
Array of strings
No match
One-element array with the original string
First piece
{ $arrayElemAt: [ { $split: [...] }, 0 ] }
Count pieces
{ $size: { $split: [...] } }
Sample check
"a,b,c" + "," → ["a", "b", "c"]
Try it
Live Preview
Watch $split turn a string into an array. Try common delimiters like comma and @.
Simulates $split (same idea as the mongosh examples below).
We store one article with a CSV tag string and an email. First we split the tags, then we split the email at @. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one article titled MongoDB Tips with a comma-separated tag string and an email address. We save it in a collection named articles.
tagCsv as stored:
"mongodb,database,tutorial"
After $split with ",":
["mongodb", "database", "tutorial"]
email as stored:
"[email protected]"
After $split with "@":
["alice", "example.com"]
How It Works
Insert once.insertOne adds a single article document to articles.
Strings stay as written.tagCsv and email are plain strings until we split them.
find() shows the same shape. What you inserted is what you get back—ready for $split next.
Example 2 — Split Comma-Separated Tags
Goal: turn tagCsv into a real tags array using "," as the delimiter.
Same stored document. Still using email: "[email protected]" from Example 1.
Split at @.$split yields ["alice", "example.com"].
Pick index 0.$arrayElemAt takes the first piece as username.
Easy check. Local part is alice; domain is index 1.
Edge Cases & Pitfalls
Check these before using $split in production pipelines.
no match
Missing delimiter → one element
If the delimiter is absent, you get [originalString], not an empty array. Plan for that in filters.
null
Null and missing become null
Missing string fields return null. Use $ifNull when you want a default string first.
delimiter
Empty delimiter is invalid
An empty string "" as delimiter fails with an error. You cannot use $split to explode into single characters that way.
spaces
Spaces stay on pieces
Splitting "a, b" on "," yields ["a", " b"]. Trim pieces or use ", " as the delimiter when needed.
Analysis
Performance Notes
Approach
Cost
Best for
$split alone
Per-document string scan
CSV tags, emails, simple paths
$split + $arrayElemAt
Split + O(1) index pick
Username / first tag extraction
Store as array field
No runtime split
Hot paths that filter tags often
Splitting is cheap for short strings. If you query tags often, consider storing a real array field instead of splitting on every read. Filter early with $match when only some documents need parsing.
Remember
Key Takeaways
Role: string → array of pieces by delimiter.
Syntax:{ $split: [ string, delimiter ] }.
One piece: wrap with $arrayElemAt.
No match: returns [originalString].
One line:$split answers “what pieces do I get when I cut this string at every delimiter?”
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"]. Use it inside aggregation expression stages such as $project and $addFields.
Use { $split: [ <string expression>, <delimiter> ] }. The first argument is the string to split; the second is the delimiter (a string, or a regex pattern on newer MongoDB versions).
MongoDB returns a one-element array containing the original string. For example, splitting "hello" by "," returns ["hello"].
$split breaks one string into an array. $concat joins multiple strings into one string. They are opposite directions for simple delimiter-based work.
If the string expression is null or missing, $split returns null. Non-string values cause the aggregation to fail. Use $ifNull when the field may be missing.
Yes. Wrap $split with $arrayElemAt and index 0 (or another index) to pick one segment.