Graph lookup in mongoDB
Posted By : Pradeep Singh Kushwah | 19-Aug-2018
Introduction:-
As we know that MongoDB is a Non-Relational Database which is written in C++, Because of Non-Relational database MongoDB doesn't support Join operation directly so that's why MongoDB gives the feature of $lookup and $graphlookup these both are the aggregation operation which is used for the Left outer join so in this blog I will tell you how we can use $graphlookup for recursive search in the same document.
Graph lookup search process is as follows:-
1.$graphLookup is an aggregation operation which works on the input document.
2.$graphLookup target the search to the collections designated by the form of parameters.
3. For-each input document, the search starts with the value designated by "
4.$graphLookup matches the start with value against the field designated by connectToField in other documents
5. For each matching document object, $graphLookup takes the value of the connectFromField and checks every document in the
6. This step continues recursively search until any matching documents are founded by
Syntax:
{ $graphLookup: { from:, startWith: , connectFromField: , connectToField: , as: , maxDepth: , depthField: , restrictSearchWithMatch: } }
so First i have a collection students which contain student record like this
{ "_id" : 1, "name" : "Dev" }
{ "_id" : 2, "name" : "Eliot", "reportsToId" : "1" }
{ "_id" : 3, "name" : "Ron", "reportsToId" : "2" }
{ "_id" : 4, "name" : "Andrew", "reportsToId" : "2" }
{ "_id" : 5, "name" : "Asya", "reportsToId" : "3" }
{ "_id" : 6, "name" : "Dan", "reportsToId" : "4" }
so every student report to any other student so if i want to search recursevely to know the hierachy i will do like this:-
db.students.aggregate( [
{
$graphLookup: {
from: "students",
startWith: "$reportsTo",
connectFromField: "reportsTo",
connectToField: "_id",
as: "reportingHierarchy"
}
}
] )
{
"_id" : 1,
"name" : "Dev",
"reportingHierarchy" : [ ]
}
{
"_id" : 2,
"name" : "Eliot",
"reportsTo" : "Dev",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" }
]
}
{
"_id" : 3,
"name" : "Ron",
"reportsTo" : "Eliot",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
]
}
{
"_id" : 4,
"name" : "Andrew",
"reportsTo" : "Eliot",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
]
}
{
"_id" : 5,
"name" : "Asya",
"reportsTo" : "Ron",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" },
{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" }
]
}
{
"_id" : 6,
"name" : "Dan",
"reportsTo" : "Andrew",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" },
{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" }
]
}
For more Information see this link https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/
Thanks
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.