$rtrim
New from version 4.0.
Not supported by Elastic cluster.
The $rtrim operator in Amazon DocumentDB is used to remove trailing characters from a string. By default, it removes trailing whitespace characters, but you can also specify a set of characters to remove by passing the chars argument.
Parameters
-
input: The input string from which to remove trailing whitespace characters. -
chars: (optional) To remove specific characters.
Example (MongoDB Shell)
The following example demonstrates how to use the $rtrim operator to remove trailing specified characters (" *") from a string.
Create sample documents
db.collection.insert([ { "name": "John Doe* ", "age": 30 }, { "name": "Jane Smith ", "age": 25 }, { "name": "Bob Johnson", "age": 35 } ]);
Query example
db.collection.aggregate([ { $project: { _id: 0, name: { $rtrim: { input: "$name", chars: " *" } }, age: 1 } } ]);
Output
[
{ age: 30, name: 'John Doe' },
{ age: 25, name: 'Jane Smith' },
{ age: 35, name: 'Bob Johnson' }
]
Code examples
To view a code example for using the $rtrim command, choose the tab for the language that you want to use: