The $ltrim operator removes leading characters from a string in MongoDB aggregation expressions. By default it strips whitespace from the left; optionally you can specify which characters to remove.
01
Left Trim
Start of string only.
02
Syntax
input + optional chars.
03
Whitespace
Default trim behavior.
04
Custom Chars
Strip prefixes like #.
05
Use Cases
Data cleaning, codes.
06
vs $trim
Left vs both sides.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $ltrim operator trims characters from the left side of a string. For example, " MongoDB" becomes "MongoDB" when you trim leading whitespace. This is useful when cleaning imported CSV data, normalizing product codes, or preparing strings for matching and display.
Think of $ltrim as MongoDB’s pipeline version of trimming only the start of a string — similar in spirit to JavaScript’s String.prototype.trimStart(), with an optional custom character set.
💡
Beginner Tip
$ltrim only affects the left end. Trailing spaces stay in place. Use $trim to clean both sides, or $rtrim for the right end only.
Foundation
📝 Syntax
The $ltrim operator takes an object with input and an optional chars field:
The chars argument defines which characters to strip from the left. MongoDB removes them one by one from the start until it hits a character not in the set (like N or P).
Example 3 — Clean Both Name and SKU in One Stage
Apply $ltrim to multiple fields in a single $addFields stage:
Choose the right trim operator for your data. $ltrim is precise when you only need to fix the start of a string.
Applications
🚀 Use Cases
Import cleanup — remove leading spaces from CSV or spreadsheet data loaded into MongoDB.
SKU and code normalization — strip prefix characters like # or 0 from identifiers.
Display formatting — prepare clean labels in $project for reports and APIs.
Consistent matching — trim before comparisons so " active" matches "active" on the left side.
🧠 How $ltrim Works
1
MongoDB evaluates input
The input expression resolves to a string from a field or literal.
Input
2
Determines characters to strip
Uses chars if provided; otherwise defaults to Unicode whitespace.
Chars
3
Removes from the left
Strips matching characters from the start until the first non-matching character.
Trim
=
📝
Clean left edge
The expression returns the string with leading characters removed.
Wrap Up
Conclusion
The $ltrim operator is a practical string-cleaning tool for MongoDB aggregation pipelines. Use it to remove leading whitespace or custom prefix characters from field values before display, matching, or further string operations.
Remember: input is required, chars is optional, and only the left side is affected. For full whitespace cleanup on both ends, use $trim.
Use $ltrim when only leading characters need removal
Omit chars for standard whitespace cleanup
Pass chars to strip known prefix symbols like #
Clean strings in $addFields before matching or grouping
Use $trim when both ends need cleaning
❌ Don’t
Expect $ltrim to remove trailing spaces (use $rtrim or $trim)
Use $ltrim as a pipeline stage — it is an expression operator
Forget that null input returns null
Confuse chars with a single prefix string — it is a character set
Trim numbers directly — convert with $toString first if needed
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $ltrim
Use these points when cleaning strings in MongoDB.
5
Core concepts
📝01
Left Trim
Start of string.
Purpose
🔢02
input Required
Field or literal.
Syntax
🛠03
chars Optional
Custom strip set.
Options
🔄04
vs $trim
Left vs both.
Compare
📑05
Data Cleaning
Imports & SKUs.
Use case
❓ Frequently Asked Questions
$ltrim removes leading characters from a string. By default it strips whitespace from the left side. You can optionally pass a chars argument to remove specific characters instead.
The syntax is { $ltrim: { input: <expression>, chars: <optional string> } }. The input is required. If chars is omitted, MongoDB trims Unicode whitespace from the left.
$ltrim removes characters from the left (start) of a string. $rtrim removes from the right (end). $trim removes from both sides. Use $ltrim when only leading characters need cleaning.
If the input expression is null, $ltrim returns null. It does not throw an error.
Use $ltrim inside expression stages such as $project, $addFields, and $set when cleaning imported data, normalizing codes, or preparing strings for display or comparison.