20 Topics
Chapter•40 Minutes

Cheat Sheet: String Operators

String expressions for concatenation, case conversion, trimming, splitting, searching, replacement, regex matching, and date parsing.

$concat

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$concat: practical example
{ $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
💡 Explanation Concatenates strings; null inputs can yield null, so use $ifNull when needed. When to use it Build display labels.
Key Takeaways
  • Concatenates strings; null inputs can yield null, so use $ifNull when needed.
  • Typical use: Build display labels.

$toLower

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$toLower: practical example
{ $project: { emailLower: { $toLower: "$email" } } }
💡 Explanation Converts a string to lowercase. When to use it Normalize values. Caution For case-insensitive search/uniqueness, consider collation and compatible indexes.
Key Takeaways
  • Converts a string to lowercase.
  • Typical use: Normalize values.

$toUpper

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: String aggregation expressions

What it does

Converts a string to uppercase.

Syntax

{$toUpper: stringExpr}

Use case

Normalize codes and labels.

$toUpper: practical example
{ $project: { code: { $toUpper: "$countryCode" } } }
💡 Explanation Converts a string to uppercase. When to use it Normalize codes and labels.
Key Takeaways
  • Converts a string to uppercase.
  • Typical use: Normalize codes and labels.

$trim

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$trim: practical example
{ $project: { clean: { $trim: { input: "$name" } } } }
💡 Explanation Removes whitespace or specified characters from both ends. When to use it Clean imported values.
Key Takeaways
  • Removes whitespace or specified characters from both ends.
  • Typical use: Clean imported values.

$ltrim

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: String aggregation expressions

What it does

Removes specified characters from the beginning.

Syntax

{$ltrim: { input, chars? }}

Use case

Remove leading padding.

$ltrim: practical example
{ $project: { clean: { $ltrim: { input: "$rawCode" } } } }
💡 Explanation Removes specified characters from the beginning. When to use it Remove leading padding.
Key Takeaways
  • Removes specified characters from the beginning.
  • Typical use: Remove leading padding.

$rtrim

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: String aggregation expressions

What it does

Removes specified characters from the end.

Syntax

{$rtrim: { input, chars? }}

Use case

Remove trailing padding.

$rtrim: practical example
{ $project: { clean: { $rtrim: { input: "$rawCode" } } } }
💡 Explanation Removes specified characters from the end. When to use it Remove trailing padding.
Key Takeaways
  • Removes specified characters from the end.
  • Typical use: Remove trailing padding.

$split

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$split: practical example
{ $project: { parts: { $split: ["$path", "/"] } } }
💡 Explanation Splits a string into an array at a delimiter. When to use it Parse simple delimited fields. Caution Not a full CSV parser.
Key Takeaways
  • Splits a string into an array at a delimiter.
  • Typical use: Parse simple delimited fields.

$strLenCP

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$strLenCP: practical example
{ $project: { characters: { $strLenCP: "$description" } } }
💡 Explanation Returns length measured in Unicode code points. When to use it Validate human-readable character counts.
Key Takeaways
  • Returns length measured in Unicode code points.
  • Typical use: Validate human-readable character counts.

$strLenBytes

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: String aggregation expressions

What it does

Returns UTF-8 byte length.

Syntax

{$strLenBytes: string}

Use case

Estimate encoded size.

$strLenBytes: practical example
{ $project: { bytes: { $strLenBytes: "$description" } } }
💡 Explanation Returns UTF-8 byte length. When to use it Estimate encoded size.
Key Takeaways
  • Returns UTF-8 byte length.
  • Typical use: Estimate encoded size.

$substrCP

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: String aggregation expressions

What it does

Extracts by Unicode code-point positions.

Syntax

{$substrCP: [string, start, count]}

Use case

Create text previews.

$substrCP: practical example
{ $project: { preview: { $substrCP: ["$description", 0, 80] } } }
💡 Explanation Extracts by Unicode code-point positions. When to use it Create text previews.
Key Takeaways
  • Extracts by Unicode code-point positions.
  • Typical use: Create text previews.