Cheat Sheet: String Operators
String expressions for concatenation, case conversion, trimming, splitting, searching, replacement, regex matching, and date parsing.
$concat
Reference group: String aggregation expressions
What it does
Concatenates strings; null inputs can yield null, so use $ifNull when needed.
Syntax
{$concat: [stringExpr, ...]}
Use case
Build display labels.
{ $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }$ifNull when needed.
When to use it
Build display labels.- Concatenates strings; null inputs can yield null, so use
$ifNullwhen needed. - Typical use: Build display labels.
$toLower
Reference group: String aggregation expressions
What it does
Converts a string to lowercase.
Syntax
{$toLower: stringExpr}
Use case
Normalize values.
Important notes
For case-insensitive search/uniqueness, consider collation and compatible indexes.
{ $project: { emailLower: { $toLower: "$email" } } }- Converts a string to lowercase.
- Typical use: Normalize values.
$toUpper
Reference group: String aggregation expressions
What it does
Converts a string to uppercase.
Syntax
{$toUpper: stringExpr}
Use case
Normalize codes and labels.
{ $project: { code: { $toUpper: "$countryCode" } } }- Converts a string to uppercase.
- Typical use: Normalize codes and labels.
$trim
Reference group: String aggregation expressions
What it does
Removes whitespace or specified characters from both ends.
Syntax
{$trim: { input, chars? }}
Use case
Clean imported values.
{ $project: { clean: { $trim: { input: "$name" } } } }- Removes whitespace or specified characters from both ends.
- Typical use: Clean imported values.
$ltrim
Reference group: String aggregation expressions
What it does
Removes specified characters from the beginning.
Syntax
{$ltrim: { input, chars? }}
Use case
Remove leading padding.
{ $project: { clean: { $ltrim: { input: "$rawCode" } } } }- Removes specified characters from the beginning.
- Typical use: Remove leading padding.
$rtrim
Reference group: String aggregation expressions
What it does
Removes specified characters from the end.
Syntax
{$rtrim: { input, chars? }}
Use case
Remove trailing padding.
{ $project: { clean: { $rtrim: { input: "$rawCode" } } } }- Removes specified characters from the end.
- Typical use: Remove trailing padding.
$split
Reference group: String aggregation expressions
What it does
Splits a string into an array at a delimiter.
Syntax
{$split: [string, delimiter]}
Use case
Parse simple delimited fields.
Important notes
Not a full CSV parser.
{ $project: { parts: { $split: ["$path", "/"] } } }- Splits a string into an array at a delimiter.
- Typical use: Parse simple delimited fields.
$strLenCP
Reference group: String aggregation expressions
What it does
Returns length measured in Unicode code points.
Syntax
{$strLenCP: string}
Use case
Validate human-readable character counts.
{ $project: { characters: { $strLenCP: "$description" } } }- Returns length measured in Unicode code points.
- Typical use: Validate human-readable character counts.
$strLenBytes
Reference group: String aggregation expressions
What it does
Returns UTF-8 byte length.
Syntax
{$strLenBytes: string}
Use case
Estimate encoded size.
{ $project: { bytes: { $strLenBytes: "$description" } } }- Returns UTF-8 byte length.
- Typical use: Estimate encoded size.
$substrCP
Reference group: String aggregation expressions
What it does
Extracts by Unicode code-point positions.
Syntax
{$substrCP: [string, start, count]}
Use case
Create text previews.
{ $project: { preview: { $substrCP: ["$description", 0, 80] } } }- Extracts by Unicode code-point positions.
- Typical use: Create text previews.