Updated March 24, 2023
Introduction to Lookup in MongoDB
MongoDB consists of aggregation pipeline, A framework based on the concept of pipelining documents. Documents enter a pipeline with multiple stages consisting of multiple operators such as match and group to aggregate data accordingly. Lookup is one of the aggregation pipeline stages which enables joining data to an input collection (The collection on which the query is currently being executed) from a lookup collection (The collection from which data is retrieved to join) and returns a document consisting of an array of matched data from lookup collection. In this topic, we are going to learn about Lookup in MongoDB.
Syntax:
Equality Match $lookup
- from: this is the collection from which documents are joined
- localField: This is the field in the input collection
- foreignField: This is the field from the lookup collection which is matched against the localField
- as: This is the field where all the matched results from lookup collection are stored in the form of an array.
Join and Uncorrelated subqueries
- Pipeline: Pipelining is staging all the matches and filters in order of execution
- let: let is used to define variables in specific expressions and access the expression’s return results.
Characteristics of $lookup
- $lookup supports uncorrelated subqueries and equality matches.
- Applications that use MongoDB for data act as clients for the MongoDB server.$lookup processes data on the server-side, making it much faster than performing multiple DB requests and processing the results on the client-side.
- $lookup scans the entire lookup collection to see if there is a match for the filters mentioned for every document in the input collection. Therefore efficient searching techniques like indexing are required for huge data sets.
- In the pipeline, the order is essential as the data gets aggregated w.r.t the operator/rule mentioned first in the pipeline.
Constraints on the usage of $lookup
- Both the collections should be in the same database.
- lookup collection should be shared
Examples of Lookup in MongoDB
Here are the examples of Lookup in MongoDB mention below
1. Single Equality join using $lookup
Create a collection of students with the following documents:
Code:
db.students .insertMany([
{“id” : 1 , “pupil” : “John” , “std” : 6, “ht” : 153 , “wt” : 43},
{“id” : 2 , “pupil” : “Jack” , “std” : 6, “ht” : 164 , “wt” : 54},
{“id” : 3 , “pupil” : “Jill” , “std” : 6, “ht” : 173 , “wt” : 69},
{“id” : 4 , “pupil” : “william” , “std” : 6}
])
Output:
Another collection of sports contains the following documents :
Code:
db.sports .insertMany([
{“id” : 1, “sport” : “Basketball” , “winner” : “John”},
{“id” : 2, “sport” : “TT” , “winner” : “Jack”},
{“id” : 3, “sport” : “Tennis” , “winner” : “John”},
{“id” : 4, “sport” : “carrom” , “winner” : “william”},
{“id” : 5, “sport” : “fencing” , “winner” : “william”},
{“id” : 6}])
Output:
The following query joins matches from sports collection to students collection based on the field value pupil and winner in students and sports respectively
Code:
db.students.aggregate([
{
$lookup:
{
from : “sports”,
localField : “pupil”,
foreignField : “winner”,
as : “games”
} } ] )
When the above query is executed, the following result is displayed.
Output:
2. $lookup with an Array
Create a collection genres with the following documents
Code:
db.geners.insertMany( [
{ _id: 1, title: "Notebook", genrelist: [ "comedy", "romance", "fiction" ]},
{ _id: 2, title: "Anabelle", genrelist: [ "horror", "fiction" ]}
])
Output:
Create another collection of movies with the following documents
Code:
db.movies.insertMany( [
{ _id: 1, name: "HarryPotter", "type": "fiction", rating: "A" },
{ _id: 2, name: "LOTR", "type": "fiction", rating: "D" },
{ _id: 3, name: "Witchcraft", "type": "horror", rating: "A" },
{ _id: 4, name: "panda", "type": "romance", rating: "A" },
{ _id: 5, name: "What is new", "type": "comedy", rating: "A" },
{ _id: 6, name: "Date", "type": "romance", rating: "D" }
])
Output:
When querying a localField which is of type array, $lookup matches every element’s value to the foreign field’s value in lookup collection. It returns an array similar to an equality match.
Code:
db.geners.aggregate([
{
$lookup:
{
from : “movies”,
localField : “genrelist”,
foreignField : “type”,
as : “movie”
} } ] ).pretty()
When the above query is executed, the following result is displayed.
Output:
3. $lookup with $mergeobjects
$lookup returns documents with from the input collections joined with the matched documents from the lookup collection, but the documents in the original input collection are added in the form of an array which consists of all the matches. $mergeobjects enables the users to modify the documents in input collection based on the filters set in $lookup.
Create a collection of groceries and insert the following documents:
Code:
db.groceries .insertMany([
{“id” : 1 , “item” : “cashew” , “qnt” : 6, “price” : 43},
{“id” : 2 , “item” : “cookie” , “qnt” : 4, “price ” : 54}
])
Output:
Create another collection stock and insert the following documents.
Code:
db.stock .insertMany([
{“id” : 1 , “item” : “cashew” , “description” : “salted cashews”, “isle” : 43},
{“id” : 2 , “item” : “cookie” , “description” : “oat and raisin cookies”, “isle ” : 54}
])
Output:
Code:
db.groceries.aggregate([
{
$lookup: {
from: "stock",
localField: "item",
foreignField: "item",
as: "fromItems"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$fromItems", 0 ] }, "$$ROOT" ] } }
},
{ $project: { fromItems: 0 } }
])
This will read all the matching documents from the stock collection, and When the above query is executed, the following result is displayed.
Output:
4. Uncorrelated Subquery
Uncorrelated subqueries are independent of the outer query. They don’t have references to the object in the outer/parent statement.
Create a collection sports
Code:
db.sports.insert([
{ "_id" : 1, "student" : "Ann Aardvark", “sports”: [ "basketball","Throwball" ] },
{ "_id" : 2, "student" : "Zoe Zebra", “sports”: [ "Tennis","TT"] },
])
Output:
Code:
db.winners.insert([
{ "_id" : 1, “sport”: "basketball", “place”: 1, date: new Date("2018-01-01") },
{ "_id" : 2, “sport”: "basketball", “place”: 4, date: new Date("2018-03-14") },
{ "_id" : 3, “sport”: "basketball", “place”:2, date: new Date("2018-07-15") },
{ "_id" : 4, “sport”: "TT", “place”: 6, date: new Date("2017-01-01") },
{ "_id" : 5, “sport”: "TT", “place”: 8, date: new Date("2017-07-16") }
])
Output:
This query matches the sport basketball and in all the document in lookup collection and picks the place and date, creates a new field win of type array and stores all the matches in the array.
Code:
db.sports.aggregate([
{
$lookup:
{
from: "winners",
pipeline: [
{ $match: { sport: "basketball" } },
{ $project: { _id: 0, date: { place: "$place", date: "$date" } } },
{ $replaceRoot: { newRoot: "$date" } }
],
as: "win"
}
}
]).pretty()
When the above query is executed, the following result is displayed.
Output:
Recommended Articles
This is a guide to Lookup in MongoDB. Here we discuss the characteristics and examples of Lookup in MongoDB along with the query and output. You can also go through our other suggested articles to learn more –