M320: Tree Pattern

The answer sheet explained the following. But why can’t one use regular expression ‘' to match the direct reports. like '/Stuart\ Spencer/’ ?

"The Materialized Paths approach falls short on:

Collect all direct reports of an employee with one single and efficient query We can use a regular expression to match all reports of a given employee. However, this regular expression query, to be efficient and use a supporting index, requires the prepending of left end side of the reports_to values. In this particular case, if we do not have the information of the root of the organization chart, the CEO name, we would not be able to create an efficient query"

1 Like

Queries using regular expressions are much slower than the ones using exact values because the query must search all index keys to find the ones that satisfy the predicate. Compare that to finding a value in a tree where you can use a binary search and narrow quickly to the value.

Alternatively, a regular expression that starts with “^” (searching at the beginning of the string) let you do binary searches.

That said, if the number of documents in a collection is very small, the impact in performance may not be that bad. However on large collections, avoid using regular expression searches.

Thanks for the feedback, we will revise the lab to make it clearer.

1 Like

While revising the Lab: Tree Patterns answers, it does not explain why the child pattern is not appropriate

1 Like

I think the Child Pattern is not ideal because it’s difficult to issue one database request to find the direct manager of a given employee.
Other two requirements can easily be fulfilled:

  • Collect all direct reports of an employee with one single and efficient query.
    -> Just query the “reports” field
  • Issue one update operation to change the reporting structure of an employee.
    -> Just update the “name” field

Please use the Report an Issue button to report this problem and it’ll go directly to the Curriculum Engineers to be fixed.
image