In MongoDB, a collection is like a container where we store related data. Each collection consists of documents, and each document has its own data.
$lookup
$lookup
is an operator in MongoDB that allows us to combine documents from different collections and the same database based on a condition. It is similar to the concept of performing left join in relational database.
In this answer, we will understand the function of $lookup
operator in detail.
The $lookup
operator takes an input collection. It matches the documents in the input collection with the documents in the from
collection, also known as the foreign collection, based on a specified localField
and foreignField
. It then adds the matched documents as an array field in the input collection.
db.inputCollection.aggregate([{$lookup: {from: "foreignCollection",localField: "localField",foreignField: "foreignField",as: "outputField"}}])
db
: A keyword that represents our current database.
inputCollection
: Our input collection.
aggregate
: The function that allows aggregation using $lookup
operator.
from
: The other foreign collection we perform join with.
localField
: The field from the input collection.
foreignField
: The field from the foreign collection.
as
: The name of the output field created in our input collection, which contains matched documents of the foreign collection.
Note: The $lookup
operator adds the documents from the foreign collection to the input collection if there is a match between the specified localField
and foreignField
in both collections.
Let's understand the working of $lookup
operator better with an example. We suppose we have a database with two collections: customers and users.
customers collection consists of the following fields:
_id
Name
2) orders collection consists of the following fields:
_id
CustomerId
Product
Now let's study these collections before and after the execution of the $lookup
operator.
Note: In MongoDB, data is represented in JSON format instead of tables. The following example shows the tabular representation to help you better understand and illustrate the example.
Our collections before applying $lookup
operator are the following:
We apply $lookup
using the following syntax:
db.customers.aggregate([{$lookup: {from: "orders",localField: "_id",foreignField: "customerId",as: "customerOrders"}}])
Our collection customers
acts as our input collection.
from
: We specify orders
as our foreign collection to perform join with.
localField
: We specify it to be the _id
field in the customers
collection.
foreignField
: We specify it to be the CustomerId
field in the orders
collection.
as
: We specify the name of the new field to be customerOrders
. This field will be added in the customers
collection.
Our collections after applying $lookup
operator will look like the following:
The following commands on the terminal demonstrate how we applied $lookup
operator in the above example:
The $lookup
operator in MongoDB allows us to combine data from multiple collections based on shared fields. By specifying the input collection, local field, foreign collection, and foreign field, we can create relationships and retrieve related data.
Free Resources