$ltrim (aggregation)
On this page本页内容
Definition定义
$ltrim
-
Removes whitespace characters, including null, or the specified characters from the beginning of a string.删除字符串开头的空白字符,包括null
或指定字符。$ltrim
has the following syntax:语法如下:{ $ltrim: { input: <string>, chars: <string> } }
The$ltrim
takes a document with the following fields:$ltrim
获取具有以下字段的文档:Field字段Description描述input
The string to trim.要修剪的字符串。The argument can be any valid expression that resolves to a string.参数可以是解析为字符串的任何有效表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。chars
Optional.可选的。The character(s) to trim from the beginning of the从input
.input
开始要修剪的字符。
The argument can be any valid expression that resolves to a string.参数可以是解析为字符串的任何有效表达式。The$ltrim
operator breaks down the string into individual UTF code pointto trim from
input
.$ltrim
运算符将字符串分解为单独的UTF代码点,以便从输入中进行修剪。
If unspecified,如果未指定,$ltrim
removes whitespace characters, including the null character.$ltrim
将删除空白字符,包括null
字符。For the list of whitespace characters, see Whitespace Characters.有关空白字符的列表,请参阅空白字符。
Behavior行为
By default,默认情况下,$ltrim
removes whitespace characters, including the null character, from the beginning of the input string:$ltrim
会从输入字符串的开头删除空白字符,包括null
字符:Example示例Results结果{ $ltrim: { input: " \n good bye \t " } }
"good bye \t "
You can override the default characters to trim using the您可以使用chars
field.chars
字段覆盖要修剪的默认字符。For example, the following trims any例如,下面将从输入字符串的开头修剪任何g
ande
from the start of the input string.g
和e
。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如果覆盖要修剪的默认字符,则可以在chars
field.chars
字段中显式包含要修剪的空白字符。For example, the following trims any space,例如,以下内容会修剪输入字符串开头的任何空格g
, ord
from the start of the input string.g
或d
。Example示例Results结果{ $ltrim: { input: " ggggoodbyeeeee ", chars: " gd" } }
"oodbyeeeee "
Whitespace Characters空白字符
By default, 默认情况下,$ltrim
removes the following characters:$ltrim
会删除以下字符:
Unicode | ||
---|---|---|
U+0000 | '0' | Null 字符 |
U+0020 | ' ' | |
U+0009 | 't' | |
U+000A | 'n' | |
U+000B | 'v' | |
U+000C | 'f' | |
U+000D | 'r' | |
U+00A0 | ||
U+1680 | ||
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 |
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 以下操作使用$ltrim
operator to remove leading whitespaces from the description
field:$ltrim
运算符从description
字段中删除前导空格:
db.inventory.aggregate([
{ $project: { item: 1, description: { $ltrim: { input: "$description" } } } }
])
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "item" : "ABC1", "description" : "product 1" }
{ "_id" : 2, "item" : "ABC2", "description" : "product 2 \n The product is in stock. \n\n " }
{ "_id" : 3, "item" : "XYZ1", "description" : null }