How to join two fields in MongoDB?

In MongoDB, you can join two fields by using the $lookup aggregation operator. This allows you to join two or more collections and return all matching documents from both collections. This is useful for retrieving data from two related collections and combining the data into a single document. The $lookup operator can also be used to perform left outer join, which allows you to include documents even if no matches exist in the other collection.


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 .

The following tutorials explain how to perform other common operations in MongoDB:

x