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,
$substrBytesor$substrCPis 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:
$strLenBytescounts bytes, not necessarily characters. For Unicode text such as Gujarati, Hindi, or emoji, the byte count can be greater than the number of characters.$strLenCPcounts 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:
0→ Strings are equal"Mital" vs "MITAL" Result: 0Both strings are considered equal because$strcasecmpignores case.- Negative value → First string comes before the second
"Apple" vs "Banana" Result: Negative value"Apple"comes before"Banana"alphabetically. - 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
| Operator | Purpose | Example |
|---|---|---|
$concat | Combines strings | "Mital" + " Goswami" |
$substr / $substrBytes | Extracts part of a string | "Mital" → "Mita" |
$toUpper | Converts to uppercase | "mital" → "MITAL" |
$toLower | Converts to lowercase | "MITAL" → "mital" |
$strLenBytes | Finds string length in bytes | "Mital" → 5 |
$strcasecmp | Compares two strings | "Mital" vs "MITAL" → 0 |
$split | Splits string into an array | "Mital Goswami" → ["Mital","Goswami"] |