M320: tree patterns: which is better for "who reports to Y?" - parent vs child reference?

Hi, in the ch4 tree patterns
Regarding ‘who reports to Y?’ query,
the lecture spells out in the bullet lists that

  • parent reference: yes (@2:58)
  • child reference: not ideal (@4:00)

I’m not sure that parent ref is better than child refs.
You’ll probably run kind of this,
1: parent ref: db.col.find({report_to: 'Y'})
2: child refs: db.col.find({id: 'Y'})

#1 will return multiple docs whereas #2 a single doc.
Then isn’t #2 child refs more optimal than #1 parent ref?

Or is it because #2 requires recursive queries on refs to get all info on each child?

Thanks

Hey @Gunho_Cho,

Please note in the video, a checkmark is used to emphasise that the pattern does support the query.

When an exclamation mark is being used, the lecturer wants to denote that it is possible to support this query, however, it may take more work in the application code, such as the use of multiple queries or there’s already an easy solution to the problem. Exclamation Mark does not mean that you cannot use that reference.

Now, the Child References pattern stores each tree node in a document; in addition to the tree node, document stores in an array the id(s) of the node’s children. Since for a node, we specify all its children, it would not be ideal to use this pattern when trying to find out “who reports to Y”. You can query for a node in the children field to find its parent node as well as its siblings.

Similarly, The Parent References pattern stores each tree node in a document; in addition to the tree node, the document stores the id of the node’s parent. So, for Y, in the document itself, we will have the id of the parent node. Hence, finding “who reports to Y” will be pretty straight forward using db.collection.findOne( { _id: "<Y>" } ).parent

I would highly suggest going over the documentation to understand how the documents are structured in both these references so as to be able to better understand the lecture. The quiz and lab immediately after this lecture should have also helped you understand the concepts better. Please feel free to reach out for anything else as well.

Regards,
Satyam

2 Likes

Hi Satyam,

I’m considering the optimal pattern given the original structure.
For example, in your comment,

Child References > You can query for a node in the children field to find its parent node as well as its siblings.

I’m not suggesting to hold parent nodes in the children field.

Let me rephrase the question.
If we use Parent Reference, we will run db.col.find({report_to: 'P'}) that will return multiple documents that has the value of ‘P’ for report_to field.
Whereas if to use Children Reference, db.col.find({id: 'P'}) that will return one document which has all children in children field.
Both can get all members who report to ‘P’ but Par Ref returns multiple documents whereas Cld Ref returns one document.
Naively thinking, returning one doc looks better but the course articulates that Cld Ref is a poor fit for the case.
Is it because an app must run follow-up queries to fetch full documents for children?

Thanks