The $sample documentation https://docs.mongodb.com/manual/reference/operator/aggregation/sample/#behavior states the following:
“If all the following conditions are met, $sample uses a pseudo-random cursor to select documents: * $sample is the first stage of the pipeline
- N is less than 5% of the total documents in the collection
- The collection contains more than 100 documents
If any of the above conditions are NOT met, $sample performs a collection scan followed by a random sort to select N documents.”
That to me sounds like if $sample comes in second stage it will perform a collection scan (not saying it actually does a collection scan, but the bold sentence make it sound like it would).
The bold sentence above can be changed to:
"If any of the above conditions are NOT met, $sample will process all input documents (that come from either a preceding aggregation stage or a collection scan) followed by a random sort to select N documents"