Docs HomeMongoDB Manual

$trim (aggregation)

On this page本页内容

Definition定义

$trim

Removes whitespace characters, including null, or the specified characters from the beginning and end of a string.删除字符串开头和结尾的空白字符,包括null或指定字符。

$trim has the following syntax:具有以下语法:

{ $trim: { input: <string>,  chars: <string> } }

The $trim takes a document with the following fields:$trim获取一个包含以下字段的文档:

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 input.要从input中修剪的字符。
The argument can be any valid expression that resolves to a string. 参数可以是解析为字符串的任何有效表达式The $trim operator breaks down the string into individual UTF code point to trim from input.$trim运算符将字符串分解为各个UTF代码点,以便从输入中进行修剪。
If unspecified, $trim removes whitespace characters, including the null character. For the list of whitespace characters, see Whitespace Characters. 如果未指定,$trim将删除空白字符,包括null字符。有关空白字符的列表,请参阅空白字符
Tip

See also: 另请参阅:

Behavior行为

  • By default, $trim removes whitespace characters, including the null character:默认情况下,$trim会删除空白字符,包括null字符:

    Example示例Results结果
    { $trim: { input: " \n good bye \t " } }"good bye"
  • You can override the default characters to trim using the chars field.您可以使用chars字段覆盖要修剪的默认字符。

    For example, the following trims any g and e from the start and end of the input. 例如,以下内容从输入的开始和结束处修剪任何geSince the input starts with a whitespace, neither character can be trimmed from the start of the string.由于输入以空白开始,因此不能从字符串的开头对两个字符进行修剪。

    Example示例Results结果
    { $trim: { input: " ggggoodbyeeeee", chars: "ge" } }" ggggoodby"
  • If overriding the default characters to trim, you can explicitly include the whitespace character(s) to trim in the chars field.如果覆盖要修剪的默认字符,则可以在chars字段中显式包含要修剪的空白字符。

    For example, the following trims any space, g, e from the start and end of the input.例如,以下内容从输入的开始和结束处修剪任何空格ge

    Example示例Results结果
    { $trim: { input: " ggggoodbyeeeee", chars: " ge" } }"oodby"

Whitespace Characters空白字符

By default, $trim removes the following whitespaces, including the null character:默认情况下,$trim会删除以下空白,包括null字符:

UnicodeEscape sequenceDescription描述
U+0000'0'Null characterNull字符
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+00A0Non-breaking space
U+1680Ogham space mark
U+2000En quad
U+2001Em quad
U+2002En space
U+2003Em space
U+2004Three-per-em space
U+2005Four-per-em space
U+2006Six-per-em space
U+2007Figure space
U+2008Punctuation space
U+2009Thin space
U+200AHair space

Example实例

Consider an inventory collection with the following documents:考虑一个包含以下文档的inventory集合:

{ "_id" : 1, "item" : "ABC1", quarter: "13Q1", "description" : " product 1" }
{ "_id" : 2, "item" : "ABC2", quarter: "13Q4", "description" : "product 2 \n The product is in stock. \n\n " }
{ "_id" : 3, "item" : "XYZ1", quarter: "14Q2", "description" : null }

The following operation uses the $trim operator to remove leading and trailing whitespaces from the description field:以下操作使用$trim运算符从description字段中删除前导空格和尾随空格:

db.inventory.aggregate([
{ $project: { item: 1, description: { $trim: { input: "$description" } } } }
])

The operation returns the following results:该操作返回以下结果:

{ "_id" : 1, "item" : "ABC1", "description" : "product 1" }
{ "_id" : 3, "item" : "XYZ1", "description" : null }
{ "_id" : 2, "item" : "ABC2", "description" : "product 2 \n The product is in stock." }