Extended $lookup

Hi @Javier_Blanco,

To try to clarify this: distinct is a Database Command. The aggregation framework uses an aggregate database command, which supports Aggregation Pipeline Stages which use Aggregation Pipeline Operators. Aggregation stages and operators are prefixed with $ (for example: $group).

You cannot call the distinct command from within the aggregate command. If you add distinct in an aggregation pipeline, it is just a value:

You can perform the equivalent processing of a distinct command in the aggregation framework using a $group aggregation stage, but I would consider the recommendations you’ve been given such as adjusting the data model to support your common queries or upgrading to MongoDB 5.0+ to take advantage of performance improvements.

Regards,
Stennie

3 Likes