On this page本页内容
$sample
uses one of two methods to obtain N random documents, depending on the size of the collection, the size of N, and $sample
’s position in the pipeline.$sample
使用两种方法之一获取N个随机文档,具体取决于集合的大小、N的大小以及$sample
在管道中的位置。
If all the following conditions are met, 如果满足以下所有条件,$sample
uses a pseudo-random cursor to select documents:$sample
使用伪随机游标选择文档:
$sample
is the first stage of the pipeline$sample
是管道的第一阶段If any of the above conditions are NOT met, 如果不满足上述任何条件,$sample
performs a collection scan followed by a random sort to select N documents. $sample
将执行收集扫描,然后进行随机排序,以选择N个文档。In this case, the 在这种情况下,$sample
stage is subject to the sort memory restrictions.$sample
阶段受排序内存限制。
Warning
$sample
may output the same document more than once in its result set. $sample
可能会在其结果集中多次输出同一文档。For more information, see Cursor Isolation.有关更多信息,请参阅游标隔离。
Given a collection named 给定一个名为users
with the following documents:users
的集合,其中包含以下文档:
The following aggregation operation randomly selects 以下聚合操作从集合中随机选择3
documents from the collection:3
个文档:
The operation returns three random documents.该操作将返回三个随机文档。