For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

$concat (expression operator)

$concat

Concatenates strings and returns the concatenated string.

$concat has this syntax:

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

The arguments can be any valid expression as long as they resolve to strings. For more information on expressions, see Expressions.

If the argument resolves to a value of null or refers to a field that is missing, $concat returns null.

The following example uses the movies collection from the sample_mflix sample dataset. For details on how to load this dataset into your deployment, see Load the sample dataset.

Use the $concat operator to concatenate the title field and the rated field with a " - " delimiter. The runtime filter limits the result to a small, representative set of documents:

db.movies.aggregate( [ { $match: { runtime: { $gt: 1000 } } }, { $project: { _id: 0, title: 1, titleRated: { $concat: [ "$title", " - ", "$rated" ] } } }, { $sort: { title: 1 } }] )
[ { title: 'Baseball', titleRated: 'Baseball - TV-PG' }, { title: 'Centennial', titleRated: null }]
Rate this page

On this page