I have data in a Mongo database collection where each document has the id of a parent. If I want to search all documents that have a particular document (I'll refer to it as P) in its ancestry (ie P is it's parent, grandparent, great grandparent, etc), what are my options to do that efficiently, and what are those options strengths and weaknesses?
I can think of the following:
- Store the whole ancestry in each document, so you can search for documents who's ancestry list contains P.
- Strengths:
- constant time look up
- Weaknesses:
- If a parent is changed, the corresponding update is O(n), where n is the number of descendants of the document who's parent changed
- Some storage overhead, O(a) where a is the average depth of a document
- Strengths:
- When searching, first build a list of the ids of P's child documents, then grandchild documents, etc. Then search all documents with those ids
- Strengths:
- No change is needed to the storage structure, no additional space overhead
- Weaknesses:
- Building up the list of ids is an O(n) operation, where n is the number of documents descendant from P
- Searching by possibly hundreds of ids might not be efficient
- Strengths:
Anyone know other techniques?