- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $ltrim (aggregation)
$ltrim (aggregation)¶
On this page
Definition¶
-
$ltrim¶ New in version 4.0.
Removes whitespace characters, including null, or the specified characters from the beginning of a string.
$ltrimhas the following syntax:The
$ltrimtakes a document with the following fields:Field Description inputThe string to trim. The argument can be any valid expression that resolves to a string. For more information on expressions, see Expressions. charsOptional. The character(s) to trim from the beginning of the
input.The argument can be any valid expression that resolves to a string. The
$ltrimoperator breaks down the string into individual UTF code point to trim frominput.If unspecified,
$ltrimremoves whitespace characters, including the null character. For the list of whitespace characters, see Whitespace Characters.
Behavior¶
By default,
$ltrimremoves whitespace characters, including the null character, from the beginning of the input string:Example Results { $ltrim: { input: " \n good bye \t " } }"good bye \t "You can override the default characters to trim using the
charsfield.For example, the following trims any
gandefrom the start of the input string. Since the input starts with a whitespace, neither character can be trimmed from the start of the string.Example Results { $ltrim: { input: " ggggoodbyeeeee", chars: "ge" } }" ggggoodbyeeeee"If overriding the default characters to trim, you can explicitly include the whitespace character(s) to trim in the
charsfield.For example, the following trims any space,
g, ordfrom the start of the input string.Example Results { $ltrim: { input: " ggggoodbyeeeee ", chars: " gd" } }"oodbyeeeee "
Whitespace Characters¶
By default, $ltrim removes the following characters:
| Unicode | Escape sequence | Description |
|---|---|---|
| U+0000 | ‘0’ | Null character |
| U+0020 | ‘ ‘ | Space |
| U+0009 | ‘t’ | Horizontal tab |
| U+000A | ‘n’ | Line feed/new line |
| U+000B | ‘v’ | Vertical tab |
| U+000C | ‘f’ | Form feed |
| U+000D | ‘r’ | Carriage return |
| U+00A0 | Non-breaking space | |
| U+1680 | Ogham space mark | |
| U+2000 | En quad | |
| U+2001 | Em quad | |
| U+2002 | En space | |
| U+2003 | Em space | |
| U+2004 | Three-per-em space | |
| U+2005 | Four-per-em space | |
| U+2006 | Six-per-em space | |
| U+2007 | Figure space | |
| U+2008 | Punctuation space | |
| U+2009 | Thin space | |
| U+200A | Hair space |