1

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?

Mike
  • 23,542
  • 14
  • 76
  • 87
  • @YuTing I need it to be in `ratePlans.parentRatePlanIds`. Using a name without a dot will lump them all together. That's not what I need. – Mike Apr 06 '22 at 02:52

0 Answers0