$rtrim
The $rtrim operator removes specified characters from the end (right) of a string.
📌 Syntax
{
"$rtrim": {
"input": <expression>,
"chars": <charsToTrim>
}
}
input: The string to trim from the rightchars: Characters to remove (defaults to whitespace)
✅ Base Example 1 – Trim Trailing Whitespace
📥 Input Document
{ "code": "SKU100 " }
📌 Expression
{ "$rtrim": { "input": "$code" } }
📤 Output
"SKU100"
✅ Base Example 2 – Trim Dashes from End
📥 Input Document
{ "slug": "tshirt---" }
📌 Expression
{
"$rtrim": {
"input": "$slug",
"chars": "-"
}
}
📤 Output
"tshirt"
🧱 Ecommerce Example – Trim Codes for Export
📌 Pipeline
[
{ "$unwind": "$items" },
{
"$project": {
"cleanCode": {
"$rtrim": {
"input": "$items.sku",
"chars": "-"
}
},
"product": "$items.name"
}
}
]
📥 Input Document
{
"items": [
{ "sku": "ABC123---", "name": "Bag" },
{ "sku": "XYZ999-", "name": "Shoes" }
]
}
📤 Output
[
{ "cleanCode": "ABC123", "product": "Bag" },
{ "cleanCode": "XYZ999", "product": "Shoes" }
]
🔧 Common Use Cases
- Remove unwanted trailing symbols
- Clean up formatting before exporting
- Strip padding or delimiters from strings
🔗 Related Operators
$ltrim,$trim,$toLower,$substr
🧠Notes
- If
charsis omitted, whitespace is removed. - Use with
$ltrimor$trimto handle full sides.