In MongoDB, the $trim
aggregation pipeline operator removes whitespace from the beginning and end 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 (-
) or periods (.
) or all s
characters, etc.
Example
Suppose we have a collection called pets
with the following document:
{ "_id" : 1, "name" : "Wagg", "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 $trim
operator to return that field with the whitespace removed.
Example:
db.pets.aggregate([
{
$project: {
name: 1,
type: { $trim: { input: "$type" } }
}
}
])
Result:
{ "_id" : 1, "name" : "Wagg", "type" : "Dog" }
As expected, the type
field has been returned without the whitespace.
You can also use the $ltrim
operator to trim the left part of the string, and the $rtrim
operator to trim the right side of the string.
There are actually quite a few characters that MongoDB deems to be whitespace characters. See MongoDB Whitespace Characters for a full list.
Trim Other Characters
The $trim
operator accepts a chars
parameter that allows you to specify which characters to trim.
Example:
db.pets.aggregate([
{
$project: {
name: { $trim: { input: "$name", chars: "g" } }
}
}
])
Result:
{ "_id" : 1, "name" : "Wa" }
It has removed both g
characters from the end 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: { $trim: { input: "$name", chars: "Wgz" } }
}
}
])
Result:
{ "_id" : 1, "name" : "a" }
In this case, I provided three characters as my chars
argument, and two of those characters happened to be at either end of the string. Therefore, those two characters were trimmed. Actually, three characters where trimmed – one W
and two g
characters.
However, be careful when doing this. Here’s what happens when I replace z
with a
in the chars
argument:
db.pets.aggregate([
{
$project: {
name: { $trim: { input: "$name", chars: "Wga" } }
}
}
])
Result:
{ "_id" : 1, "name" : "" }
The whole string has disappeared. It has trimmed not only the W
and g
from each end of the string, but it has also trimmed the a
from the string – even though it was in the middle of the string.
Trimming Numbers
The $trim
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: { $trim: { input: "$weight", chars: "0" } }
}
}
])
Result:
Error: command failed: { "ok" : 0, "errmsg" : "$trim 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, even though the $trim
operator requires its input to be a string.
If we really wanted to remove the zero, 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: { $trim: { input: { $toString: "$weight" }, chars: "0" } }
}
}
])
Result:
{ "_id" : 1, "name" : "Wagg", "weight" : "2" }
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: { $trim: { input: { $toString: "$weight" }, chars: "0" } } }
}
}
])
Result:
{ "_id" : 1, "name" : "Wagg", "weight" : 2 }