On this page本页内容
$trim
¶New in version 4.0.版本4.0中的新功能。
Removes whitespace characters, including null, or the specified characters from the beginning and end of a string.删除字符串开头和结尾的空白字符(包括null
)或指定字符。
$trim
has the following syntax:语法如下所示:
The $trim
takes a document with the following fields:$trim
获取具有以下字段的文档:
input |
|
chars |
|
$trim
removes whitespace characters, including the null character:$trim
删除空白字符,包括空字符:
{ $trim: { input: " \n good bye \t " } } |
"good bye" |
chars
field.chars
字段覆盖要修剪的默认字符。
For example, the following trims any 例如,下面将从输入的开始和结束修剪任何g
and e
from the start and end of the input. g
和e
。Since the input starts with a whitespace, neither character can be trimmed from the start of the string.由于输入以空格开头,因此两个字符都不能从字符串开头修剪。
{ $trim: { input: " ggggoodbyeeeee", chars: "ge" } } |
" ggggoodby" |
chars
field.chars
字段中显式包含要修剪的空白字符。
For example, the following trims any space, 例如,以下命令将从输入的开始和结束修剪任何空格、g
, e
from the start and end of the input.g
、e
。
{ $trim: { input: " ggggoodbyeeeee", chars: " ge" } } |
"oodby" |
By default, 默认情况下,$trim
removes the following whitespaces, including the null character:$trim
删除以下空白,包括空字符:
Unicode | ||
---|---|---|
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 |
Consider an 考虑使用以下文档集合inventory
collection with the following documents:inventory
:
The following operation uses the 以下操作使用$trim
operator to remove leading and trailing whitespaces from the description
field:$trim
运算符从description
字段中删除前导空格和尾随空格:
The operation returns the following results:操作返回以下结果: