$ltrim (aggregation)
Nesta página
Definição
$ltrim
Removes whitespace characters, including null, or the specified characters from the beginning of a string.
$ltrim
tem a seguinte sintaxe:{ $ltrim: { input: <string>, chars: <string> } } O
$ltrim
pega um documento com os seguintes campos:CampoDescriçãoinput
A string a ser cortada. O argumento pode ser qualquer expressão válida que produza uma string. Para mais informações sobre expressões, consulte Operadores de Expressão.
chars
Optional. The character(s) to trim from the beginning of the
input
.O argumento pode ser qualquer expressão válida que produza uma string. O operador divide a
$ltrim
cadeia de caracteres em ponto código UTF individual parainput
cortar.Se não for especificado, remove os caracteres de espaço em branco, inclusive o caractere
$ltrim
nulo. Para obter a lista de caracteres de espaço em branco, consulte Caracteres de espaço em branco.
Comportamento
By default,
$ltrim
removes whitespace characters, including the null character, from the beginning of the input string:ExemploResultados{ $ltrim: { input: " \n good bye \t " } }
"good bye \t "
Você pode substituir os caracteres padrão para cortar usando o campo
chars
.For example, the following trims any
g
ande
from the start of the input string. Since the input starts with a whitespace, neither character can be trimmed from the start of the string.ExemploResultados{ $ltrim: { input: " ggggoodbyeeeee", chars: "ge" } }
" ggggoodbyeeeee"
Se substituir os caracteres padrão para cortar, você pode incluir explicitamente os caracteres de espaço em branco a serem cortados no campo
chars
.For example, the following trims any space,
g
, ord
from the start of the input string.ExemploResultados{ $ltrim: { input: " ggggoodbyeeeee ", chars: " gd" } }
"oodbyeeeee "
Caracteres do espaço em branco
By default, $ltrim
removes the following characters:
Unicode | Sequência de escape | Descrição |
---|---|---|
U+0000 | '0' | Caractere nulo |
U+0020 | ' ' | Espaço |
U+0009 | 't' | Aba horizontal |
U+000A | 'n' | Feed de linha/nova linha |
U+000B | 'v' | Aba vertical |
U+000C | 'f' | Feed de formulários |
U+000D | 'r' | Quebra de linha |
U+00A0 | Espaço ininterrupto | |
U+1680 | Marca de espaço Ogham | |
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 | Espaço da figura | |
U+2008 | Espaço de pontuação | |
U+2009 | Espaço fino | |
U+200A | Espaço para o cabelo |
Exemplo
Considere uma coleção inventory
com os seguintes documentos:
{ "_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:
db.inventory.aggregate([ { $project: { item: 1, description: { $ltrim: { input: "$description" } } } } ])
A operação retorna os seguintes resultados:
{ "_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 }