Docs HomeMongoDB Manual

$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:如果前面的任何一个条件为false,则$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.随机排序受排序内存限制

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: 另请参阅:

$rand (aggregation)