Open In App

MongoDB – $concat Operator

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

What is the MongoDB $concat Operator?

The $concatoperator 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:

{ $concat: [ <expression1>, <expression2>, … ] }

Parameters:

  • <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.
  • The result of the $concat operation is a single string made up of the concatenated values from the provided expressions.

Key Features of MongoDB – $concat Operator

Here, the arguments passed in this operator can be any valid expression until they resolve to strings.

  • If the entered argument resolves to null, then this operator will returnnull.
  • If the entered argument refers to a missing field, then this operator will return null.
  • The $concat operator in MongoDB is used to combine strings.

Why Use MongoDB $concat Operator?

The MongoDB $concat operator is a versatile tool that enables string manipulation in several ways:

  1. Combining Multiple Fields: Concatenate data from multiple fields into one.
  2. Creating Dynamic Strings: Dynamically create messages, labels, or formatted outputs.
  3. Efficient Data Transformation: Customize output by adding prefixes, suffixes, or formatting string fields to meet your application’s requirements.

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:

  • Concatenating First and Last Names: Combine the first name, middle name, and last name to create a full name for a user.
  • Adding Static Prefixes or Suffixes: Prefix or suffix text to data, such as adding “Hello, ” before a name or ” – Employee” after a title.
  • Building Custom Messages: Create personalized messages, for example, for a report or a notification.

Examples of MongoDB $concat Operator

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.

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

demo database and collection creation

Example 1: Concatenating Strings Using $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:

example 1 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 Documents

In 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:

example 2 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.

Key Takeaways About $concat Operator

  • String Concatenation: The $concat operator is primarily used to concatenate two or more strings or expressions into a single string.
  • Aggregation Pipeline: It’s mainly used in the $project stage to create new fields by combining string data from multiple sources.
  • Flexible: It can handle field references, string literals, and complex expressions, giving you flexibility when combining data.
  • Null Handling: If an argument resolves to null or references a missing field, the operator will return null.
  • Combining with Other Operators: The $concat operator can be combined with other aggregation operators (e.g., $trim, $toUpper, $toLower) to further manipulate strings.

Conclusion

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.



Next Article

Similar Reads