You can use the following syntax to concatenate strings from two fields into a new field in MongoDB:
db.myCollection.aggregate([ { $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } }, { $merge: "myCollection" } ])
This particular example concatenates the strings from “field1” and “field2” into a new field titled “newfield” and adds the new field to the collection titled myCollection.
The following example shows how to use this syntax in practice with a collection teams with the following documents:
db.teams.insertOne({team: "Mavs", conference: "Western", points: 31}) db.teams.insertOne({team: "Spurs", conference: "Western", points: 22}) db.teams.insertOne({team: "Rockets", conference: "Western", points: 19}) db.teams.insertOne({team: "Celtics", conference: "Eastern", points: 26}) db.teams.insertOne({team: "Cavs", conference: "Eastern", points: 33}) db.teams.insertOne({team: "Nets", conference: "Eastern", points: 38})
Example: Concatenate Strings in MongoDB
We can use the following code to concatenate the strings from the “team” field and the “conference” field into a new field titled “teamConf” and add this field to the teams collection:
db.teams.aggregate([ { $project: { teamConf: { $concat: [ "$team", " - ", "$conference" ] } } }, { $merge: "teams" } ])
Here’s what the updated collection now looks like:
{ _id: ObjectId("62013d8c4cb04b772fd7a90c"), team: 'Mavs', conference: 'Western', points: 31, teamConf: 'Mavs - Western' } { _id: ObjectId("62013d8c4cb04b772fd7a90d"), team: 'Spurs', conference: 'Western', points: 22, teamConf: 'Spurs - Western' } { _id: ObjectId("62013d8c4cb04b772fd7a90e"), team: 'Rockets', conference: 'Western', points: 19, teamConf: 'Rockets - Western' } { _id: ObjectId("62013d8c4cb04b772fd7a90f"), team: 'Celtics', conference: 'Eastern', points: 26, teamConf: 'Celtics - Eastern' } { _id: ObjectId("62013d8c4cb04b772fd7a910"), team: 'Cavs', conference: 'Eastern', points: 33, teamConf: 'Cavs - Eastern' } { _id: ObjectId("62013d8c4cb04b772fd7a911"), team: 'Nets', conference: 'Eastern', points: 38, teamConf: 'Nets - Eastern' }
Notice that every document has a new field titled “teamConf” that contains the concatenation of the “team” and “conference” fields.
For this particular example we chose to concatenate the two strings together using a dash as a separator.
However, we could choose to concatenate the two strings without any separator value in between them.
The following code shows how to do so:
db.teams.aggregate([ { $project: { teamConf: { $concat: [ "$team", "$conference" ] } } }, { $merge: "teams" } ])
Here’s what the updated collection would look like:
{ _id: ObjectId("62013d8c4cb04b772fd7a90c"), team: 'Mavs', conference: 'Western', points: 31, teamConf: 'MavsWestern' } { _id: ObjectId("62013d8c4cb04b772fd7a90d"), team: 'Spurs', conference: 'Western', points: 22, teamConf: 'SpursWestern' } { _id: ObjectId("62013d8c4cb04b772fd7a90e"), team: 'Rockets', conference: 'Western', points: 19, teamConf: 'RocketWestern' } { _id: ObjectId("62013d8c4cb04b772fd7a90f"), team: 'Celtics', conference: 'Eastern', points: 26, teamConf: 'CelticsEastern' } { _id: ObjectId("62013d8c4cb04b772fd7a910"), team: 'Cavs', conference: 'Eastern', points: 33, teamConf: 'CavsEastern' } { _id: ObjectId("62013d8c4cb04b772fd7a911"), team: 'Nets', conference: 'Eastern', points: 38, teamConf: 'NetsEastern' }
Notice that the new field titled “teamConf” contains the concatenation of the “team” and “conference” fields without any separator values between them.
Note: You can find the complete documentation for the $concat function here.
Additional Resources
The following tutorials explain how to perform other common operations in MongoDB:
MongoDB: How to Check if Field Contains a String
MongoDB: How to Add a New Field
MongoDB: How to Remove a Field