Performant way to count unique subdocuments for each document

Could you please read Formatting code and log snippets in posts and then update your post?

It would make it easier to read, understand and we could cut-n-paste it to experiment with. An alternative is make it a playground at https://mongoplayground.net/

Ad Thanks Vance