MongoDB $rtrim

In MongoDB, the $rtrim aggregation pipeline operator removes whitespace from the 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 periods (.), exclamation marks (!), etc from the end 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 $rtrim operator to return that field with the whitespace removed from the right part of the string.

Example:

db.pets.aggregate([
  { 
    $project: { 
      name: 1, 
      type: { $rtrim: { input: "$type" } } 
    } 
  }
])

Result:

{ "_id" : 1, "name" : "Wag!!!", "type" : "   Dog" } 

As expected, the type field has been returned without the whitespace at the end. The whitespace at the beginning still remains.

You can also use the $ltrim operator to trim the left 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 $rtrim operator accepts a chars parameter that allows you to specify which characters to trim.

Example:

db.pets.aggregate([
  { 
    $project: {
      name: { $rtrim: { input: "$name", chars: "!" } } 
    } 
  }
])

Result:

{ "_id" : 1, "name" : "Wag" } 

In this case we included the chars parameter with an exclamation mark (!), which resulted in all three exclamation marks being removed from the end of the string.

Trim Multiple Characters

You can trim multiple characters by including them all in the chars argument.

Example:

db.pets.aggregate([
  { 
    $project: { 
      name: { $rtrim: { input: "$name", chars: "!g" } } 
    } 
  }
])

Result:

{ "_id" : 1, "name" : "Wa" } 

In this case, I provided two characters as my chars argument, and two of those characters happened to be at the end 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: { $rtrim: { input: "$name", chars: "!agW" } } 
    } 
  }
])

Result:

{ "_id" : 1, "name" : "" }

The whole string has disappeared. It has trimmed not only the ! and g from the string, but it has also removed the W and a characters.

Trimming Numbers

The $rtrim 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: { $rtrim: { input: "$weight", chars: "0" } } 
    } 
  }
])

Result:

Error: command failed: {
	"ok" : 0,
	"errmsg" : "$rtrim 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 $rtrim 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: { $rtrim: { input: { $toString: "$weight" }, chars: "0" } }
    }
  }
])

Result:

{ "_id" : 1, "name" : "Wag!!!", "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: { $rtrim: { input: { $toString: "$weight" }, chars: "0" } } }
    }
  }
])

Result:

{ "_id" : 1, "name" : "Wag!!!", "weight" : 2 }