Find one document and reduce elements inside an array property

I have a scenario where I will only fetch one document and then reduce several array properties in that document so each contains one or more array elements.

Example:

{
    array_property1: 
    [ 
        {
            language_code: "EN-US"
            ...
        },
        {
            language_code: "EN-UK"
            ...
        }
    ],
    ...
}

I am able to reduce the number of array elements using $elemMatch, however it will only result in one array element. Is there any other method to achieve an array reduce for one or more objects instead of just one when using find command?

The reason for this is because I have heard that using find instead of aggregation in this scenario would yield a better performance and would also be much easier to maintain because it is only one document I am retriving using an objectID.

You may have already figured this out based on your other question but modelling data as arrays where each element has a unique field is sub optimal - you have to use $filter or $map in an aggregation, when you have a unique identifier like this a model of

{
   en-us: { ... },
   en-uk: { .. }
}

allows the find projection to do the job which uses much less CPU/is faster.

However - it’s very rare that this level of optimisation matters so I’m really curious about your use case. I worked on a schema once where this Array to Object flip mattered but in that case there were 10,000 items at each level not 20 or so.

1 Like