Node.js MongoDB Join
In MongoDB, to join data from multiple collections in Node.js, we can use the $lookup
aggregation pipeline stage. The $lookup
stage allows us to perform a left outer join on two collections and merge the results into a single document. In this tutorial, we'll discuss how to use $lookup
in Node.js to join data from multiple collections.
Syntax
The syntax for using $lookup
in a MongoDB aggregation pipeline in Node.js is as follows:
db.collection('collection1').aggregate([
{ $lookup:
{
from: "collection2",
localField: "field1",
foreignField: "field2",
as: "output"
}
}
])
from
: specifies the name of the collection to join.localField
: specifies the field from the input document to use for the join.foreignField
: specifies the field from the documents of the "from" collection to use for the join.as
: specifies the name of the output field that will contain the joined documents.
Example
Let's say we have two collections in MongoDB, "orders" and "customers". "orders" contains a "customerId" field that references the "_id" field on the "customers" collection. We want to join these two collections in Node.js to retrieve the customer details for each order. Here's how we can do it:
db.collection('orders').aggregate([
{ $lookup:
{
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customer"
}
}
])
Now, for each order document, the "customer" field will contain an array of customer documents that match the "customerId" field.
Output
When we run the above example, the output will be an array of documents that contains the joined data from the "orders" and "customers" collections.
Explanation
In the example above, we used the $lookup
stage in a MongoDB aggregation pipeline in Node.js to join data from the "orders" and "customers" collections. We specified the "from" field to be "customers", the "localField" to be "customerId" from the "orders" collection, and the "foreignField" to be "_id" from the "customers" collection. We also specified the "as" field as "customer" to indicate that we want to merge the joined documents into a "customer" field in the output.
Use
Using $lookup
in a MongoDB aggregation pipeline in Node.js is useful when you need to fetch data from multiple collections and merge them into a single document. It provides a way to perform a left outer join on two collections, based on a common field or set of fields.
Important Points
$lookup
is an expensive operation, and you should use it only when necessary.- The
$lookup
stage returns an empty array if no matching documents are found in the joined collection. - Make sure to index the fields that are used for the join to speed up the query.
Summary
In this tutorial, we discussed how to use $lookup
in a MongoDB aggregation pipeline in Node.js to join data from multiple collections. We covered the syntax, example, output, explanation, use, and important points of using $lookup
in Node.js. With this knowledge, you can now join data from multiple collections in MongoDB using Node.js and perform a left outer join on two collections based on a common field or set of fields.