Mismatch condition in sample stage video example?

collection name: nycFacilities

  • This collection had more than 100 documents (condition 1)

  • sample size (N) is greater than 5% (condition 2)

  • $stage is the first in Aggregation Pipeline.

db.nycFacilities([
{
$sample:{size:200}
}
]).pretty()

if meets the 3 conditions, it will take the pseudo-random-selector but condition 2 failed for pseudo-random-selection. (conditions listed above)

how it selects the pseudo-random-selector for selecting documents within the collection?

Hi @Nagababu_Upputuri,
This is the expected behavior, as mentioned in the $sample aggregation-pipeline-stage documentation:

If any of the above conditions are NOT met, $sample performs a collection scan followed by a random sort to select N documents. In this case, the $sample stage is subject to the sort memory restrictions.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

@ SourabhBagrecha Thanks for your response :smiley: