$split (Split String)

Last updated: 2026-10-09

$split (Split String)

$split breaks a string into an array of substrings by a delimiter — a staple for CSV-ish text, tag lists, and paths. The delimiter must be a non-empty string; an empty delimiter splits by individual code points. It pairs well with array operators like $arrayElemAt and $size. If either argument is null or missing, the result is null.

CategoryString
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $split: [ <string-expression>, <delimiter> ] }

⚙️ Parameters

stringRequired; the string or expression to split.
delimiterRequired, non-empty; an empty string splits by code points.

Returns:An array of substrings; null if either argument is null.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $split: split comma-separated tags into an array
 db.products.aggregate([
  { $project: { tags: { $split: [ '$tagStr', ',' ] } } }
])

Output:

{ "_id": 1, "tags": [ "sale", "new", "hot" ] }
{ "_id": 2, "tags": [ "featured" ] }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QCan the delimiter be an empty string?
AYes — an empty string splits by individual code points.
QHow to get the first element after splitting?
AUse $arrayElemAt with index 0.
QWhat if the delimiter never occurs?
AThe array contains only the original string.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏