Table of Contents
To concatenate strings from two fields in MongoDB, you can use the $concat operator in the aggregation pipeline. This operator allows you to combine multiple fields or strings into a single field. By specifying the fields or strings to be concatenated, you can easily merge them together and create a new concatenated string field in your MongoDB collection. This method is useful for creating dynamic and customized fields based on existing data in your database. Additionally, it allows for efficient and flexible data manipulation and retrieval within your MongoDB database.
MongoDB: Concatenate Strings from Two Fields
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' } Note: You can find the complete documentation for the $concat function .
Additional Resources
The following tutorials explain how to perform other common operations in MongoDB:
Cite this article
stats writer (2024). How can I concatenate strings from two fields in MongoDB?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-from-two-fields-in-mongodb/
stats writer. "How can I concatenate strings from two fields in MongoDB?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-from-two-fields-in-mongodb/.
stats writer. "How can I concatenate strings from two fields in MongoDB?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-from-two-fields-in-mongodb/.
stats writer (2024) 'How can I concatenate strings from two fields in MongoDB?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-concatenate-strings-from-two-fields-in-mongodb/.
[1] stats writer, "How can I concatenate strings from two fields in MongoDB?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I concatenate strings from two fields in MongoDB?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
