MongoDB is a NoSQL type of database and it is foreseen that these databases do not follow RDBMS (Relational Database Management systems) and thus do not provide extensive JOIN methods. However, the JOIN concept of MongoDB is inspired by SQL databases, because initially, MongoDB itself did not provide any joining method. You can JOIN two collections in MongoDB with the help of the $lookup operator of aggregation.
A single collection may not describe the data stored inside it; one collection may contain such fields that must be linked with the field of another collection to describe that field. For this, you can use the $lookup operator of the aggregation method. However, after the introduction of the $lookup operator, the users can enjoy joining access in MongoDB.
In this article, we have briefly explained the usage of the $lookup operator, and a few examples are presented that show the joining mechanism of MongoDB.
How join works in MongoDB
This section provides the basic working mechanism of the $lookup operator, that is used in the aggregation method to perform joining in MongoDB. The syntax is provided below:
The terms used in syntax are explained here:
– collection-name: The collection name on which you are present or the $lookup operator is applied
– from The collection, you are targeting to join
– localField: This represents the field of a document in a current collection that will be used to match with other collections
– foreign field: The field of the collection (to be joined) that can represent the whole document. (unique id)
– as: This contains an array field that is created after joining
The upcoming section will demonstrate joining two collections in a MongoDB database.
As mentioned earlier, with the help of the $lookup operator, you can match two collections of a database. So, to perform this operation, you must need two collections from a database.
In this post, we have used “staff” and “info” as a collection of a “linuxhint” database. Be careful while selecting a collection, because you can only join two collections that reside in the same database.
The “staff” collection contains the following documents inside it: the command mentioned below is used to retrieve documents of a “staff” collection.
And content of second collection “info” is displayed by issuing the following command:
How to join two collections in MongoDB
In this section, you will learn to perform a join in MongoDB. For that, we have performed the action to join the “staff” collection with the “info” collection.
In the below-mentioned command, the aggregate method exercises the $lookup operator to get the information from both collections and will join them based on the following condition:
If the “localField” of “staff” collection matches the “foreignField” of “info” collection.
The joining can be seen in the output section of the below-pasted image. We have used the following label to provide a better understanding.
The “Input” and “output” labels show the inserted command and its result respectively. The data of both collections after joining is labeled also and an array field “Staff_info” contains the data of “info” collection after joining.
MongoDB is well-known because of the extensive support for the processing of data inside a database. However, it does not support any dedicated method to join collections like in SQL-based databases. Alternative to Join, MongoDB supports a $lookup operator that can be used in the aggregation method to perform the left join. In this tutorial of the MongoDB series, we have explained the working phenomenon of the $lookup operator in the aggregation method. By following this guide, a Mongo enthusiast would be able to join one collection with another..