MongoDB $concat Operator (original) (raw)

Last Updated : 16 Apr, 2026

The $concat operator in MongoDB concatenates multiple strings or expressions into a single string within the aggregation pipeline, enabling efficient string formatting and reporting during data transformation.

Syntax

{ $concat: [ , , ... ] }

Features of $concat Operator

Some features are listed below:

Use Cases for MongoDB $concat Operator

Here are a few practical scenarios where the $concat operator shines:

Examples of MongoDB $concat Operator

We'll use a sample collection called employee in the GeeksforGeeks database, which contains employee information.

Screenshot-2026-02-14-113110

Example 1: Concatenating Strings Using $concat

Formatting a string by combining a static message with the department field. Here, we concatenate "My department is:" string with the value of the department field.

db.employee.aggregate([
{
$project: {
"name.first": 1,
_id: 0,
dept: { concat:["Mydepartmentis:","concat: ["My department is: ", "concat:["Mydepartmentis:","department"] }
}
}
])

**Output:

Screenshot-2026-02-14-113233

Example 2: Concatenating Strings in Embedded Documents

Find the full name of the employees by concatenating the values of the name.first, name.middle, and name.last fields.

db.employee.aggregate([
{
$project: {
fullname: {
concat:["concat: ["concat:["name.first", " ", "$name.middle", " ", "$name.last"]
}
}
}
])

**Output:

Screenshot-2026-02-14-113428

Usage of $concat Operator

Here are some usage of $concat operator: