$ltrim is a MongoDB aggregation expression that removes characters from the left (start) of a string. Default: leading whitespace. Optional chars: a set of characters to strip instead.
Remember
$ltrim = trim from the left only
{ $ltrim: { input: <string> } }
{ $ltrim: { input: <string>, chars: <string> } }
default chars → whitespace (incl. null)
custom chars → each character is a trim candidate (a set, not one substring)
{ $ltrim: { input: " hello" } } → "hello"
{ $ltrim: { input: "00042", chars: "0" } } → "42"
null / missing input → null
right side untouched
For custom leading characters, add chars (for example "0").
Remember: trimming stops at the first character not listed in chars.
Piece
Required?
Role
input
Yes
String to clean
chars
No
Character set to strip from the left
Result
—
New string (right side unchanged)
Pseudocode
Pseudocode
s = evaluate(input)
if s is null or missing:
result = null
else:
set = chars if provided else whitespace
while s starts with a character in set:
remove that leading character
result = s
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while cleaning leading characters.
A label is stored as " hello" (two leading spaces). We want "hello".
Calculation
input: " hello"
$ltrim default (whitespace):
char[0] = ' ' → trim
char[1] = ' ' → trim
char[2] = 'h' → stop (not whitespace)
Result:
"hello"
Trailing spaces (if any) would stay — $ltrim only touches the left.
Code
MongoDB Examples
We use one document with a padded label and a zero-padded code. First we store it, then we trim spaces, then we trim leading zeros. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one row with a messy label " hello" and a code "00042".
Set a custom char set.chars: "0" means “trim zeros from the left”.
Strip while matching. Three leading 0s are removed.
Stop at 4.4 is not in chars, so trimming ends → "42".
Project a clean shape. Output shows original code and cleaned code.
Edge Cases & Pitfalls
Check these before using $ltrim in production pipelines.
chars
chars is a set, not a prefix
chars: "01" trims any mix of 0 and 1 from the left—not only the exact substring "01".
block
First non-matching char stops trim
If the string starts with a space and chars is "0", nothing is removed. Include the space in chars, or trim whitespace first.
side
Right side is untouched
Trailing spaces stay. Use $rtrim or $trim when you need the other end too.
null
Null input returns null
Missing or null strings become null, not an empty string.
Analysis
Performance Notes
Approach
Cost
Best for
$ltrim in pipeline
Cheap per document
Cleaning imported strings
Trim on write
Write cost, cheaper reads
Fields queried/compared often
Very long strings
Scans from the left
Still fine for normal text fields
Trimming is inexpensive compared with large sorts or joins. Clean early when later stages depend on exact string matches.
Remember
Key Takeaways
Role: remove leading characters from a string.
Default: whitespace; optional chars set.
Stops early: first non-matching character ends the trim.
Siblings:$rtrim (right), $trim (both).
One line:$ltrim answers “what does this string look like without its leading junk?”
Frequently Asked Questions
$ltrim removes characters from the beginning (left) of a string. By default it strips whitespace. Optionally pass chars to remove a set of custom characters instead. Use it inside aggregation stages such as $project and $addFields.
The syntax is { $ltrim: { input: <string>, chars: <optional string> } }. input is required. If chars is omitted, MongoDB trims Unicode whitespace (including null) from the left.
$ltrim removes from the left only. $rtrim removes from the right only. $trim removes from both sides. Use $ltrim when only leading characters need cleaning.
A character set. MongoDB treats each UTF code point in chars as a character that can be trimmed from the start, not as one fixed prefix string.
If input is null or refers to a missing field, $ltrim returns null. It does not throw an error.
Trimming stops at the first character that is not in chars. If the string starts with a space and chars is "0", the leading space blocks zero-trimming. Include the space in chars, or trim whitespace first.