-
Type: Question
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.4.14, 3.6.3
-
Component/s: None
-
None
-
Query Optimization
Just got acquainted with the $graphLookup aggregation, but it seems a little clunky for my main use case. So I have a combined question and/or feature request:
My use case is I want to fetch all descendants for a node in a tree structure. But I want them as plain documents. Thus I'm forced to something like:
db.nodes.aggregate([ {$match: {_id: 'STARTID'}}, { $graphLookup: { from: 'nodes', startWith: '$_id', connectFromField: 'children', connectToField: '_id', as: 'descendants', }, }, {$unwind: '$descendants'}, {$replaceRoot: {newRoot: '$descendants'}} ])
It would be much more natural to me if the $graphLookup operator could operate without a starting collection, i.e. something like (I know this doesn't work now):
db.aggregate([ { $graphLookup: { from: 'nodes', startWith: 'STARTID', connectFromField: 'children', connectToField: '_id' }, } ])
So, is this something that can be done today in some similar fashion. Otherwise, it'd be nice to have.