We have a use case where we need to sort results from the $search stage in Atlas aggregation based on a virtual/computed field.
The requirement is that $search must be the first stage in the pipeline. However, we need to add a new field using $addFields before $search, to compute a value for sorting. So, we would like to know any possibility to do that?
For example, adding an index position field to sort documents matching a given array of IDs first:
There is no way to do this via aggregation today. As you note, $search must be the first stage in a pipeline and does not work with any post-processed data. We are working on a feature, however, that will allow you to enrich your data as it is indexed. When available, you will be able to use $addFields to create synthetic fields as your data in ingested. You could then sort on this synthetic field. This may help here.
Until then, the best option is to create/manage this field application-side.