I have the following stage in my aggregation pipeline:
{
$graphLookup: {
from: 'rateplans',
startWith: '$ratePlans.parentRatePlanId',
connectFromField: 'parentRatePlanId',
connectToField: '_id',
as: 'ratePlans.parentRatePlanIds',
depthField: 'sortOrder',
},
},
This adds the ratePlans.parentRatePlanIds
field, however they are out of order. I need them to be sorted by depth (sortOrder
).
I tried the following:
{
$unwind: '$ratePlans.parentRatePlanIds'
},
{
$sort: {
_id: 1,
'ratePlans.parentRatePlanIds.sortOrder': 1
},
},
{
$group: {
_id: '$_id',
// other fields
ratePlans: { $first: '$ratePlans' },
'ratePlans.parentRatePlanIds': { $push: '$ratePlans.parentRatePlanIds' },
}
},
However I get the error:
The field name 'ratePlans.parentRatePlanIds' cannot contain '.'
How can I do $graphLookup
and add the result to a subdocument and then sort it so that the order is persistent?