$sample (aggregation)

On this page本页内容

Definition定义

$sample

Randomly selects the specified number of documents from the input documents.从输入文档中随机选择指定数量的文档。

The $sample stage has the following syntax:$sample阶段语法如下:

{ $sample: { size: <positive integer N> } }

N is 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个文档:

  • $sample is the first stage of the pipeline.是管道的第一阶段。
  • N is 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:如果之前的任何条件为假,$sample

  • Reads all documents that are output from a preceding aggregation stage or a collection scan.读取从前一聚合阶段或集合扫描输出的所有文档。
  • Performs a random sort to select N documents.执行随机排序以选择N个文档。
Note注意

Random sorts are subject to the sort memory restrictions.随机排序受排序内存限制

MMAPv1 May Return Duplicate DocumentsMMAPv1可能会返回重复文档

If you are using the:如果您正在使用:

  • MMAPv1 storage engine, $sample may return the same document more than once in the result set.MMAPv1存储引擎,$sample可能在结果集中多次返回同一文档。
  • WiredTiger or in-memory storage engine, $sample does not return duplicate documents. 存储引擎中,$sample不返回重复文档。WiredTiger is the default storage engine as of MongoDB 3.2.WiredTiger是MongoDB 3.2的默认存储引擎。

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.该操作返回三个随机文档。

Tip提示
See also: 参阅:
←  $replaceWith (aggregation)$search (aggregation) →