Uses of Lookup In MongoDb
Posted By : Pradeep Singh Kushwah | 21-Apr-2018
Introduction:-
In MongoDB, 3.2v has delivered $lookup operator in aggregation framework which may be utilized to carry out LEFT join. Please notice that MongoDB data operation isn't always like working facts in a relational database so it's far always advisable to have applicable data to be within the same report those often the usage of together. however, in a certain case, it's far required to put in two specific collections due to numerous like, to keep away from facts duplication for which we've heavy update operations, use in a couple of collections and many others. Join is a diffrence between NoSQL and SQL database wherein as MongoDB $lookup Aggregation operator is effective and useful to get be part of for two collections like doing it in RDBMS.
Syntax:-
db.(collection_Name).aggregation([
{
$lookup:{
from: "(foreign_collection)",
localField : "(local_field)",
foreignField : "(foreign_collection_field_name)",
as : "(key_name_to_appear_in_result)"
}
}
])
If you are from Relational DataBase Management System world then you will be expecting this as separate Document link all fields from the foreign and local collections but NO, all foreign documents will be introduced as an array of nearby collection file. because all foreign Document introduced in the report you would need to use best if a number of foreign relative files are constrained and now not be making the size of end result document to exceed document length limit.
Let say we have two collections college and students like this
College collection:
{
"id":"01",
"collegeName":"SRGOC",
"address" : "Kota",
"students":["012","124","156"]
},
{
"id":"02",
"collegeName":"IIITM",
"address" : "Gwalior",
"students":["012","123","198"]
}
Students collection:-
{"_id":"012":"name":"rahul","rollNo":159}, {"_id":"124":"name":"vikas","rollNo":160}, {"_id":"123":"name":"mohit","rollNo":162}, {"_id":"156":"name":"sarjib","rollNo":161}, {"_id":"198":"name":"sachin","rollNo":165}, {"_id":"201":"name":"pradeep","rollNo":168}
Now retrive the college with students details:
db.college.aggregate({
$lookup:
{
from: "students",
localField: "studentIds",
foreignField : "_id",
as: "students"
}
});
So Result will be like this:-
{
"_id":"01",
"collegeName":"SRGOC",
"address" : "Kota",
"students":[
{"_id":"012":"name":"rahul","rollNo":159},
{"_id":"124":"name":"vikas","rollNo":160},
{"_id":"156":"name":"sarjib","rollNo":161}
]
},
{
"_id":"02",
"collegeName":"IIITM",
"address" : "Gwalior",
"students":[
{"_id":"012":"name":"rahul","rollNo":159},
{"_id":"123":"name":"mohit","rollNo":162},
{"_id":"198":"name":"sachin","rollNo":165}
]
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pradeep Singh Kushwah
Pradeep is an accomplished Backend Developer with in-depth knowledge and hands-on experience in various cutting-edge technologies. He specializes in Core Java, Spring-Boot, Optaplanner, Angular, and databases such as MongoDB, Neo4j, Redis, and PostgreSQL. Additionally, he has worked with cloud services like AWS and Google Cloud, and he has experience with monitoring tools such as Datadog and Raygun. Pradeep has honed his skills in API Implementations, Integration, optimization, Webservices, Development Testings, and deployments, code enhancements, and has contributed to company values through his deliverables in various client projects, including Kairos, Slick Payroll, Captionlabs, and FarmQ. He is a creative individual with strong analytical skills and a passion for exploring and learning new technologies.