On this page本页内容
$rtrim
New in version 4.0.在版本4.0中新增。
Removes whitespace characters, including null, or the specified characters from the end of a string.从字符串末尾删除空白字符(包括null
)或指定字符。
$rtrim
has the following syntax:语法如下:
{ $rtrim: { input: <string>, chars: <string> } }
The $rtrim
takes a document with the following fields:$rtrim
获取包含以下字段的文档:
input | |
chars |
|
By default, 默认情况下,$rtrim
removes whitespace, including the null character, from the end of the input string:$rtrim
会从输入字符串的末尾删除空白,包括空字符:
{ $rtrim: { input: " \n good bye \t " } } | " \n 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 end of the input string. g
和e
。Since the input ends with a whitespace, neither character can be trimmed from the end of the string.由于输入以空格结尾,因此不能从字符串末尾修剪任何字符。
{ $rtrim: { 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 or 例如,以下命令将修剪输入字符串末尾的任何空格或e
from the end of the input string.e
。
{ $rtrim: { input: " ggggoodbyeeeee ", chars: "e " } } | " ggggoodby" |
By default, 默认情况下,$rtrim
removes the following whitespace, including the null character:$rtrim
会删除以下空白,包括空字符:
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
集合:
{ "_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 以下操作使用$rtrim
operator to remove trailing whitespace from the description
field:$rtrim
运算符从description
字段中删除尾随空格:
db.inventory.aggregate([ { $project: { item: 1, description: { $rtrim: { 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." } { "_id" : 3, "item" : "XYZ1", "description" : null }