Last Updated : 15 Jul, 2025
MongoDB, a powerful NoSQL database, offers a wide range of tools to manage and manipulate data. Among these tools, the MongoDB $concat
operator stands out as a powerful tool for string manipulation, especially within the aggregation pipeline. By using $concat
, developers can easily concatenate multiple strings or expressions, allowing for efficient data transformation and reporting.
The $concat
operator in MongoDB is mainly used within the$project
stage of an aggregation pipeline to combine multiple strings or expressions. It can take any number of string expressions as arguments and concatenate them into a single string. Additionally, $concat
accepts other expressions such as functions or operators and enables complex string concatenation logic.
Syntax:
Parameters:{ $concat: [ <expression1>, <expression2>, ... ] }
<expression1>, <expression2>, ...
: These can be any valid expressions that resolve to strings. The $concat
operator takes multiple arguments, allowing the combination of string literals, fields, or other expressions.$concat
operation is a single string made up of the concatenated values from the provided expressions.Here, the arguments passed in this operator can be any valid expression until they resolve to strings.
$concat
operator in MongoDB is used to combine strings. $concat
Operator?
The MongoDB $concat
operator is a versatile tool that enables string manipulation in several ways:
With this operator, developers can easily create custom fields, build dynamic content, and produce formatted output based on document data.
Common Use Cases for MongoDB$concat
Operator
Here are a few practical scenarios where the $concat
operator shines:
Let's explore some practical examples that showcase the MongoDB $concat operator in action. We'll use a sample collection called employee in the GeeksforGeeks database, which contains employee information.
$concat
In this example, we are finding the department of the employees. Here, we concatenate "My department is:" string with the value of the department field.
Query:
db.employee.aggregate([
... {$project: {"name.first": 1, _id: 0, dept:
... {$concat: ["My department is: ", "$department"]}}}])
Output:
Explanation: This query extracts the first name of each employee and concatenates a custom string with the department field to create a personalized message indicating the employee's department.
Example 2: Concatenating Strings in Embedded DocumentsIn this example, we are going to find the full name of the employees by concatenating the values of the name.first, name.middle, and name.last fields.
Query:
db.employee.aggregate([
... {$project: {fullname:
... {$concat: ["$name.first", "$name.middle", "$name.last"]}}}])
Output:
Explanation: The query concatenates the first
, middle
, and last
name fields of the employee into a single string fullname
, providing a complete name of the employee as a new field in the output.
$concat
Operator
$concat
operator is primarily used to concatenate two or more strings or expressions into a single string.null
or references a missing field, the operator will return null
.$concat
operator can be combined with other aggregation operators (e.g., $trim
, $toUpper
, $toLower
) to further manipulate strings.The $concat
operator in MongoDB offers a flexible solution for string concatenation within aggregation pipelines. Whether we're combining multiple fields into a single string, adding prefixes or suffixes or performing complex transformations, the $concat
operator provides a powerful way to manipulate string data in MongoDB. By mastering this operator and understanding its usage in different contexts, you can unlock powerful ways to work with string data in MongoDB.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4