In MongoDB, the $ltrim
aggregation pipeline operator removes whitespace from the beginning of a string. This includes the null character.
It can also remove any character specified. For example, you could use it to remove all hyphen characters (-
) from the beginning of a string.
Example
Suppose we have a collection called pets
with the following document:
{ "_id" : 1, "name" : "-Wag", "type" : " Dog ", "weight" : 20 }
We can see that the type
field includes white space on both sides of the word Dog
. We can use the $ltrim
operator to return that field with the whitespace removed from the left part of the string.
Example:
db.pets.aggregate([
{
$project: {
name: 1,
type: { $ltrim: { input: "$type" } }
}
}
])
Result:
{ "_id" : 1, "name" : "-Wag", "type" : "Dog " }
As expected, the type
field has been returned without the whitespace at the beginning. The whitespace at the end still remains.
You can also use the $rtrim
operator to trim the right part of the string, and the $trim
operator to trim both sides of the string.
There are quite a few characters that MongoDB deems to be whitespace characters. See MongoDB Whitespace Characters for a full list.
Trim Other Characters
The $ltrim
operator accepts a chars
parameter that allows you to specify which characters to trim.
Example:
db.pets.aggregate([
{
$project: {
name: { $ltrim: { input: "$name", chars: "-" } }
}
}
])
Result:
{ "_id" : 1, "name" : "Wag" }
In this case we included the chars
parameter with a hyphen (-
), which resulted in the hyphen being removed from the start of the word.
Trim Multiple Characters
You can trim multiple characters by including them all in the chars
argument.
Example:
db.pets.aggregate([
{
$project: {
name: { $ltrim: { input: "$name", chars: "W-" } }
}
}
])
Result:
{ "_id" : 1, "name" : "ag" }
In this case, I provided two characters as my chars
argument, and two of those characters happened to be at the start of the string. Therefore, those two characters were trimmed.
However, be careful when doing this. Here’s what happens when I include all characters:
db.pets.aggregate([
{
$project: {
name: { $ltrim: { input: "$name", chars: "W-ag" } }
}
}
])
Result:
{ "_id" : 1, "name" : "" }
The whole string has disappeared. It has trimmed not only the -
and W
from the string, but it has also removed the a
and g
characters.
Trimming Numbers
The $ltrim
operator works on strings. If we try to trim the weight
field, we get an error. This is because the weight
field is a number, not a string.
db.pets.aggregate([
{
$project: {
name: 1,
weight: { $ltrim: { input: "$weight", chars: "2" } }
}
}
])
Result:
Error: command failed: { "ok" : 0, "errmsg" : "$ltrim requires its input to be a string, got 20 (of type double) instead.", "code" : 50699, "codeName" : "Location50699" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
The error tells us that we provided a double, and that the $ltrim
operator requires its input to be a string.
If we really wanted to remove the 2, we’d need to convert it to a string first. We can do that with either the $convert
or $toString
operator.
Example:
db.pets.aggregate([
{
$project: {
name: 1,
weight: { $ltrim: { input: { $toString: "$weight" }, chars: "2" } }
}
}
])
Result:
{ "_id" : 1, "name" : "-Wag", "weight" : "0" }
We can get it back to a double by using either the $convert
or $toDouble
operator.
Full example:
db.pets.aggregate([
{
$project: {
name: 1,
weight: { $toDouble: { $ltrim: { input: { $toString: "$weight" }, chars: "2" } } }
}
}
])
Result:
{ "_id" : 1, "name" : "-Wag", "weight" : 0 }