MongoDB join query

The aggregation framework allows joins between MongoDB collections, but effective indexing is critical

One of the key tenants of MongoDB schema design is to design to avoid the need for joins. Data is joined all the time inside of application code of course, but originally there was no way to perform joins within the server itself.

This changed in 3.2 with the introduction of the $lookup operator within the aggregation framework. The $lookup operator performs the equivalent of a left outer join – eg: it retrieves matching data from another document and returns null data if no match is found.

Here’s an example using some of the data in our sample collections.

MongoDB join query
MongoDB join query
MongoDB join query
MongoDB join query

MongoDB join query
MongoDB join query
MongoDB join query
MongoDB join query

Price
View Courses

360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

  • From: It states the collection within the identical database for executing the join with but with Sharded Collection Restrictions.
  • LocalField: It states the field from the documents input to the stage of $lookup which operates an equivalence match on the local-field to the foreign field from the documents of the collection ‘from’. When an input document has no local-field value then this operator will give the field to have a null value for further matching purposes.
  • foreignField: It states the field from the documents in the ‘from’ collection which operates an equivalence match on the foreignField to the localField from the input documents. When a document in the collection ‘from’ has no value for the foreignField then this operator will give the field to have a null value for further matching purposes.
  • As: It states the name of the new array field for adding to the input documents. Also, the new array field includes the matching documents from the collection ‘from’. But when the stated name already prevails in the input document then the prevailing field will be overwritten.

In the case of the SQL concept, the result of a JOIN operation is always a different row that associates all fields present from the parent and foreign tables. But in MongoDB, the case is variant where the output documents are supplemented as an array of native collection documents.

How to join two collections in MongoDB?

For joining MongoDB collections, we require to implement the operator as $lookup, which is defined as a stage executing a left outer join with other collection and also aids to filter data info from joined documents.
Suppose a user needs to fetch all the students with grades, then the below query can be written:

Students.aggregate([{
$lookup: {
From: ‘Grades’,
LocalField: ‘Student_id’,
foreignField: ‘Stud_id’,
as: ‘Student_grade’
}}]);

We can see in the above query that the initial parameter named ‘from’ states the collection that is to be joined with the present one, on the other side the ‘localField’ parameter states the key prevailing in the present collection that is to be matched with the foreign key in another collection by means of ‘foreignField’ parameter. Here, Student_id from Students and stud_id from Grades are matched as well as joined for retrieving the data. Also, the end parameter ‘as’ is added as a code-named to the data.

It must be noted that here in every record of result from lookup, also the data from the joined table appears inside an array as the primary element, every one row from the output data of the query above that will display like,

{_ID: String, Quantity: Number, Student_id: String, Student_grade: Array, Stud_id: String, Student_name: String}

Here, in the grades array, the initial element will be the data that the query joined.

Example

Let us illustrate few examples to show the joining of two collections in MongoDB explained as follows:
Suppose we have two collections created named students and marks as,

Students:

{“_id”: 1, “name”: “Nikhil Singh”, “age”: 25, “grade”: “A”}
{“_id”: 2, “name”: “Kiran Sharma”, “age”:20, “grade”: “B”}
{“_id”: 3, “name”: “Krishna Raj”, “age”: 19, “grade”: “A”}
db.Students.find()

MongoDB join query

Marks:

{“_id”: 1, “English”: “A”, “Maths”: “B”, “Science”: “A”}
{“_id”: 2, “English”: “C”, “Maths”: “A”, “Science”: “B”}
{“_id”: 3, “English”: “B”, “Maths”: “C”, “Science”: “A”}
db.Grades.find()

MongoDB join query

We can fetch the students’ marks with corresponding grades by means of the $lookup operator using the JOIN methodology defined below:

Db.getCollection(‘Students’).aggregate([{
$lookup:
{
From: “Grades”,
localField: “_id”,
foreignField: “_id”,
as: “StudentMarks”
}
}])

On execution, the output will provide the following results,

{“_id”: 1, “name”: “Nikhil Singh”, “age”: 25, “grade”: “A”, “StudentMarks”: [{“_id”: 1, “English”: “A”, “Maths”: “B”, “Science”: “A”}]}

MongoDB join query

The structure of the written query may decide the performance duration. Like for instance, when there are several documents in one collection over the other, then aggregation is needed from one collection with smaller documents followed by lookup in the one having various documents. This type of lookup performed for the selected field from the smaller documents collection is rather finest and proceeds smaller time than executing numerous lookups for a selected field present in the collection having various documents. Thus, it is sensible to place the smaller collection initially.

But the order of the databases does not trouble in case of a relational database as maximum of the SQL interpreters includes optimizers that provide access to additional information to select which one must be the primary one.

In this MongoDB, to simplify the JOIN operation we may require to implement an index. Since all MongoDB documents include an _id key that for a relational DBM is measured as the primary key. An index delivers a better chance of minimizing the quantity of data which requires to be retrieved besides associating the operation when applied in the $lookup foreign key.

Conclusion

JOIN operator is one of the basic distinctive features available between SQL database and NoSQL database. In SQL databases, a user can execute a JOIN operation between two tables inside identical or different databases.

While the case in MongoDB for JOIN is varied, MongoDB lets JOIN operations perform between two collections implemented in a similar database.

This is a guide to MongoDB Join Two Collections. Here we discuss definition, How to join two collections in MongoDB? along with examples respectively. You may also have a look at the following articles to learn more –

How do I join a query in MongoDB?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.

Does MongoDB support query joins?

Join Collections MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.

How to write inner join query in MongoDB?

How to Perform MongoDB Inner Join operation?.
from: The name of the collection to be joined..
localField and foreignField: To join the collections using $lookup, a field from both collections is required. ... .
as: The connection between two collections in a result..

Can we join 2 collections in MongoDB?

Yes, you can join 2 collections with Aggregation Framework and $unionWith stage.