Joins in MongoDB
1. Left Join
- A left join in MongoDB combines matching documents from the primary (“left“) collection with documents from the secondary (“right“) collection.
- It returns all documents from the primary collection along with related documents from the secondary collection.
db.books.aggregate([
{
$lookup: {
from: "authors",
localField: "authorId",
foreignField: "_id",
as: "author"
}
}
]);
Output:
Explanation:
- In the above Query, We have use aggregation pipeline $lookup stage to perform a left join between the books collection and the authors collection based on matching values between the authorId field in the books collection and the _id field in the authors collection.
- The result is an aggregation output where each document from the books collection includes an additional field named author containing an array of matching documents from the authors collection.
2. Right Join
- In a right join MongoDB reverses the typical roles of the primary and secondary collections.
- It is similar to the left join but we are reversing the input documents like in left-join we combine the authors collection to the books collection but in right-join we combine the books collection into the authors collection.
db.authors.aggregate([
{
$lookup: {
from: "books",
localField: "bookId",
foreignField: "_id",
as: "book"
}
}
]);
Output:
Explanation:
- In the above Query, It matches documents from the authors collection where the bookId field corresponds to the _id field in the books collection. The result is an array named book containing related book documents for each author entry.
- This output array represents authors along with their associated books. Each object in the array contains an authorId, name, and their bookId. If an author has a book, it appears in the book array within the object, showing the bookId, name, and the authorId. For authors without any associated books, the book array is empty ([]), as seen with “Peter Thiel” and “Kristina Chodorow” in the provided example.
3. Inner Join
- In MongoDB, an inner join operation combines matching documents from two collections based on specified conditions and resulting in a set of documents that intersect.
- This process involves matching documents from the primary collection with those from the secondary collection using a common field. The result includes only the documents where a match is found in both collections.
db.books.aggregate([
{
$lookup: {
from: "authors",
localField: "authorId",
foreignField: "_id",
as: "author"
}
},
{
$match: {
"author": { $ne: [] }
}
}
]);
Output:
Explanation:
- In the above Query, It basically brings together information from the books and authors collections by finding matches based on the authorId field in the books collection and the _id field in the authors collection.
- This way, you get details about books along with their respective authors. The output only includes books with valid authors. If there’s a book without a matching author, it simply won’t show up in the result.
- In this case, there are 4 documents in the books collection, but some of them does not have a corresponding author in the authors collection. As a result, only 3 documents are returned in the output.
How to Perform the SQL Join Equivalent in MongoDB?
In database management, with the rise of NoSQL databases like MongoDB, the approach to handling data relationships has evolved. MongoDB’s document–oriented nature and schema–less design provide a different perspective on joins compared to SQL.
In this article, we’ll learn about How to Perform the SQL Join Equivalent in MongoDB by understanding their types and performing various queries using Aggregation pipelines.