MongoDB $toUpper Operator

To understand it better, let’s look at some examples of MongoDB $toUpper operator.

In the following examples, we are working with:

Database: w3wiki
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.

Using $toUpper Operator Example

In this example, we are going to convert the value of the department field in uppercase and assign the result of the $toUpper operator as a value of dept field.

db.employee.aggregate([
... {$project: {dept: {$toUpper: "$department"}}}])

Using $toUpper Operator in the Embedded Document Example

In this example, we are going to convert the value of the name.first field in uppercase and assign the result of the $toUpper operator as a value of firstName field.

db.employee.aggregate([
... {$project: {firstName: {$toUpper: "$name.first"}}}])

MongoDB $toUpper Operator

MongoDB $toUpper operator converts the given string to uppercase.

Similar Reads

$toUpper Operator in MongoDB

The $toUpper Operator in MongoDB is used in the aggregation pipeline stages to convert a string to uppercase and return the result....

Syntax

{ $toUpper: }...

MongoDB $toUpper Operator

To understand it better, let’s look at some examples of MongoDB $toUpper operator....

KeyTakeAways About MongoDB $toUpper Operator

The $toUpper operator is used in the aggregation pipeline stages to convert a string to uppercase and return the result. It accepts an expression as an argument, which can be any expression that resolves to a string. This means $toUpper can be used on non-string fields as well, as long as they can be converted to a string. If the argument provided to $toUpper resolves to null, it will return an empty string. Its behavior for non-ASCII characters may not be as expected. The $toUpper operator is designed for use within the aggregation framework and cannot be used directly in update operations to modify documents in a collection. The $toLower operator, which converts a string to lowercase works similarly to $toUpper....

Contact Us