I have a collection of comments for a forum-like app; each comment has a parentId
, and there can be comment threads of arbitrary length.
What I want to do is, starting from a root comment, get a single chain of sub-comments.
I tried something like this, but this returns a whole tree of comments:
$graphLookup: {
from: "comments",
startWith: "$_id",
connectFromField: "_id",
connectToField: "parentId",
as: "children",
maxDepth: 10,
}
Is there a way to limit the breadth to 1 at each level, so starting with the root comment, it finds only 1 child of the root, and then finds only 1 child of that comment, and so on?
As far as I can tell, I cannot do this with $graphLookup, but maybe there is a way to do this with other aggregation stages?
Or will I have to make multiple queries from my server?