How to simplify $set and $project in one step?

I’d like to simplify $set (aka $addFields) and $project because they look redundant

Is there maybe an operator allowing to rewrite entirely the $$CURRENT doc to achieve the same result of those 2 steps?

coll.aggregate([
    { $match: { ...} },
    { $lookup: { .... },
    { $unwind: '$accounts' },
    { $set: { _id: '$accounts._id', deleted: true, deletedAt: date } },
    { $project: { _id: 1, deleted: 1, deletedAt: 1 } },
    { $merge: { into: 'accounts', whenNotMatched: 'discard' } },
  ])

Perhaps try replaceRoot

coll.aggregate([
  { $match: { ... } },
  { $lookup: { ... } },
  { $unwind: '$accounts' },
  { 
    $replaceRoot: { 
      newRoot: { 
        $mergeObjects: [
          { deleted: true, deletedAt: date }, 
          { _id: '$accounts._id' }
        ] 
      } 
    }
  },
  { $merge: { into: 'accounts', whenNotMatched: 'discard' } }
]);
`

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.