$sample (aggregation)
On this page本页内容
Definition定义
$sample-
Randomly selects the specified number of documents from the input documents.从输入文档中随机选择指定数量的文档。The$samplestage has the following syntax:$sample阶段具有以下语法:{ $sample: { size: <positive integer N> } }Nis the number of documents to randomly select.是要随机选择的文档数。
Behavior行为
If all of the following conditions are true, 如果以下所有条件都成立,$sample uses a pseudo-random cursor to select the N documents:$sample将使用伪随机游标来选择N个文档:
$sampleis the first stage of the pipeline.是管道的第一阶段。Nis less than 5% of the total documents in the collection.不到集合中文档总数的5%。The collection contains more than 100 documents.该集合包含100多个文档。
If any of the previous conditions are false, 如果前面的任何一个条件为$sample:false,则$sample:
Reads all documents that are output from a preceding aggregation stage or a collection scan.读取从上一个聚合阶段或集合扫描输出的所有文档。Performs a random sort to select执行随机排序以选择Ndocuments.N个文档。
Random sorts are subject to the sort memory restrictions.随机排序受排序内存限制。
Example实例
Given a collection named 给定一个名为users with the following documents:users的集合,其中包含以下文档:
{ "_id" : 1, "name" : "dave123", "q1" : true, "q2" : true }
{ "_id" : 2, "name" : "dave2", "q1" : false, "q2" : false }
{ "_id" : 3, "name" : "ahn", "q1" : true, "q2" : true }
{ "_id" : 4, "name" : "li", "q1" : true, "q2" : false }
{ "_id" : 5, "name" : "annT", "q1" : false, "q2" : true }
{ "_id" : 6, "name" : "li", "q1" : true, "q2" : true }
{ "_id" : 7, "name" : "ty", "q1" : false, "q2" : true }
The following aggregation operation randomly selects 以下聚合操作从集合中随机选择3 documents from the collection:3个文档:
db.users.aggregate(
[ { $sample: { size: 3 } } ]
)
The operation returns three random documents.该操作返回三个随机文档。