Definition
$concatConcatenates strings and returns the concatenated string.
$concathas 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
nullor refers to a field that is missing,$concatreturnsnull.
Example
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 } ]