String Aggregation Operators in MongoDB

String aggregation operators are used to perform various operations on textual/string data stored in MongoDB document fields.

They are commonly used inside the Aggregation Pipeline, especially with the $project stage, to modify, combine, compare, or analyze string values.

Syntax

db.collection.aggregate([
   {
      $project: {
         fieldName: { stringOperator: "$fieldName" }
      }
   }
])

1. $concat

The $concat operator is used to combine two or more strings into a single string.

Example

Suppose the collection contains:

{
   firstName: "Mital",
   lastName: "Goswami"
}

Query:

db.students.aggregate([
   {
      $project: {
         fullName: {
            $concat: ["$firstName", " ", "$lastName"]
         }
      }
   }
])

Output:

{
   fullName: "Mital Goswami"
}

Use: Joining first name and last name.


2. $substr

The $substr operator is used to extract a part of a string based on its starting position and length.

In current MongoDB versions, $substrBytes or $substrCP is generally preferred.

Example

db.students.aggregate([
   {
      $project: {
         result: {
            $substrBytes: ["$name", 0, 5]
         }
      }
   }
])

If:

name: "Mital Goswami"

Output:

{
   result: "Mital"
}

Use: Extracting a specific portion of a string.


3. $toUpper

The $toUpper operator converts a string into uppercase letters.

Example

db.students.aggregate([
   {
      $project: {
         name: { $toUpper: "$name" }
      }
   }
])

Input:

name: "Mital Goswami"

Output:

{
   name: "MITAL GOSWAMI"
}

Use: Converting text to uppercase.


4. $toLower

The $toLower operator converts a string into lowercase letters.

Example

db.students.aggregate([
   {
      $project: {
         name: { $toLower: "$name" }
      }
   }
])

Input:

name: "Mital Goswami"

Output:

{
   name: "mital goswami"
}

Use: Converting text to lowercase.


5. $strLenBytes

The $strLenBytes operator returns the number of bytes used to represent a string.

Example

db.students.aggregate([
   {
      $project: {
         nameLength: {
            $strLenBytes: "$name"
         }
      }
   }
])

For:

name: "Mital"

Output:

{
   nameLength: 5
}

For English ASCII characters, normally 1 character = 1 byte.

Use: Finding the byte length of a string.

Note: $strLenBytes counts bytes, not necessarily characters. For Unicode text such as Gujarati, Hindi, or emoji, the byte count can be greater than the number of characters. $strLenCP counts Unicode code points.


6. $strcasecmp

The $strcasecmp operator compares two strings without considering the difference between uppercase and lowercase letters.

Example

db.students.aggregate([
   {
      $project: {
         result: {
            $strcasecmp: ["$name1", "$name2"]
         }
      }
   }
])

Suppose:

name1: "Mital"
name2: "MITAL"

Output:

{
   result: 0
}

The result is:

  • 0 → Both strings are equal.
  • Negative value → First string comes before second string.
  • Positive value → First string comes after second string.

Use: Comparing two strings without case sensitivity.

For $strcasecmp, the result can be understood simply as follows:

  1. 0 → Strings are equal "Mital" vs "MITAL" Result: 0 Both strings are considered equal because $strcasecmp ignores case.
  2. Negative value → First string comes before the second "Apple" vs "Banana" Result: Negative value "Apple" comes before "Banana" alphabetically.
  3. Positive value → First string comes after the second "Mango" vs "Apple" Result: Positive value "Mango" comes after "Apple" alphabetically.

Easy way to remember

0 = Same
− = First comes Before
+ = First comes After


7. $split

The $split operator divides a string into an array of substrings using a specified delimiter.

Example

db.students.aggregate([
   {
      $project: {
         words: {
            $split: ["$name", " "]
         }
      }
   }
])

If:

name: "Mital Goswami"

Output:

{
   words: ["Mital", "Goswami"]
}

Here, " " is the delimiter.

Another example:

{
   $split: ["$email", "@"]
}

For:

mital@gmail.com

Output:

["mital", "gmail.com"]

Use: Dividing a string into multiple parts.


Summary Table

OperatorPurposeExample
$concatCombines strings"Mital" + " Goswami"
$substr / $substrBytesExtracts part of a string"Mital" → "Mita"
$toUpperConverts to uppercase"mital" → "MITAL"
$toLowerConverts to lowercase"MITAL" → "mital"
$strLenBytesFinds string length in bytes"Mital" → 5
$strcasecmpCompares two strings"Mital" vs "MITAL" → 0
$splitSplits string into an array"Mital Goswami" → ["Mital","Goswami"]

By admin