Lab: Tree patterns - understanding '2nd task'

hello everybody,

im looking at this:

“Collect all direct reports of an employee with one single and efficient query.”

and also corresponding answer details explanation of it:

“In this example, querying all of our employees for anyone with the value of “Stuart Spencer” for the name of their manager, reports_to.name will give us a list of all direct reports”

not sure i understand this completely, please feel free to share your take on this topic…

thanks for looking into this much appreciated :slight_smile:

Hey @Asifuzzaman_Bappy,

As given in the solution, using the example of “Jon Yullin” and his manager “Stuart Spencer” , we would get the following document:


{
  "_id": ObjectId("123414123"),
  "name": "Jon Yullin",
  "role": "Intern",
  "department": {
    "name": "Engineering",
    "id": ObjectId("988576342364")
  },
  "reports_to": { "id": ObjectId("123414123") , "name": "Stuart Spencer" },
}

The reports_to field in this document tells us who the manager of this person is. This is the field that we can use for our second question ie. Collect all direct reports of an employee with one single and efficient query, by simply using the following query:

db.employees.find({"reports_to.name": "Stuart Spencer"})

This will tell us all the people who report to Stuart as anyone who reports to him will have Stuart Spencer in the reports_to field. In this case, only Jon Yullin’s document is returned as only he reports to Stuart.

Let us know if there is any further doubt about this. I also recommend going through the documentation to cement the concepts of Tree Patterns: Modelling Using Tree Patterns With Parent Reference

Regards,
Satyam

thanks a lot for explaining this to me, i assume it’s this statement that causing me confusion somewhat! but i see that now “id” is referring to ‘Jon’ and its report can be directly accessible through a simple .find()

i was not considering it for some reason, and somehow relating this to ‘spencer’!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.