The $rtrim operator removes trailing characters from a string in MongoDB aggregation expressions. By default it strips whitespace from the right; optionally you can specify which characters to remove.
01
Right Trim
End of string only.
02
Syntax
input + optional chars.
03
Whitespace
Default trim behavior.
04
Custom Chars
Strip suffixes like /.
05
Use Cases
URLs, imports, codes.
06
vs $ltrim
Right vs left end.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $rtrim operator trims characters from the right side of a string. For example, "MongoDB " becomes "MongoDB" when you trim trailing whitespace. This is useful when cleaning imported data, normalizing URLs, or removing trailing punctuation from codes.
Think of $rtrim as MongoDB’s pipeline version of trimming only the end of a string — similar in spirit to JavaScript’s String.prototype.trimEnd(), with an optional custom character set.
💡
Beginner Tip
$rtrim only affects the right end. Leading spaces stay in place. Use $trim to clean both sides, or $ltrim for the left end only.
Foundation
📝 Syntax
The $rtrim operator takes an object with input and an optional chars field:
The chars argument defines which characters to strip from the right. MongoDB removes them one by one from the end until it hits a character not in the set.
Example 3 — Clean Both Name and URL in One Stage
Apply $rtrim to multiple fields in a single $addFields stage:
$rtrim is precise when you only need to fix the end of a string. For both sides, use $trim when that page is available in the series.
Applications
🚀 Use Cases
Import cleanup — remove trailing spaces from CSV or spreadsheet data loaded into MongoDB.
URL normalization — strip trailing slashes so links compare and deduplicate correctly.
Display formatting — prepare clean labels in $project for reports and APIs.
Consistent matching — trim before comparisons so "active " matches "active" on the right side.
🧠 How $rtrim 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 right
Strips matching characters from the end until the first non-matching character from the right.
Trim
=
📝
Clean right edge
The expression returns the string with trailing characters removed.
Wrap Up
Conclusion
The $rtrim operator is a practical string-cleaning tool for MongoDB aggregation pipelines. Use it to remove trailing whitespace or custom suffix characters from field values before display, matching, or further string operations.
Remember: input is required, chars is optional, and only the right side is affected. For full whitespace cleanup on both ends, use $trim. Next in the series: $sampleRate.
Use $rtrim when only trailing characters need removal
Omit chars for standard whitespace cleanup
Pass chars: "/" to normalize URL paths
Clean strings in $addFields before matching or grouping
Pair with $ltrim or $trim when both ends need work
❌ Don’t
Expect $rtrim to remove leading spaces (use $ltrim or $trim)
Use $rtrim as a pipeline stage — it is an expression operator
Forget that null input returns null
Confuse chars with a single suffix 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 $rtrim
Use these points when cleaning strings in MongoDB.
5
Core concepts
📝01
Right Trim
End of string.
Purpose
🔢02
input Required
Field or literal.
Syntax
🛠03
chars Optional
Custom strip set.
Options
🔄04
vs $ltrim
Right vs left.
Compare
📑05
Data Cleaning
URLs & imports.
Use case
❓ Frequently Asked Questions
$rtrim removes trailing characters from a string. By default it strips whitespace from the right side. You can optionally pass a chars argument to remove specific characters instead.
The syntax is { $rtrim: { input: <expression>, chars: <optional string> } }. The input is required. If chars is omitted, MongoDB trims Unicode whitespace from the right.
$rtrim removes characters from the right (end) of a string. $ltrim removes from the left (start). $trim removes from both sides. Use $rtrim when only trailing characters need cleaning.
If the input expression is null, $rtrim returns null. It does not throw an error.
Use $rtrim inside expression stages such as $project, $addFields, and $set when cleaning imported data, normalizing URLs, or preparing strings for display or comparison.