OPTIONS

$sample (aggregation)

On this page

Definition

$sample

New in version 3.2.

Randomly selects the specified number of documents from its input.

The $sample stage has the following syntax:

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

Behavior

In order to get N random documents:

  • If N is greater than or equal to 5% of the total documents in the collection, $sample performs a collection scan, performs a sort, and then select the top N documents. As such, the $sample stage is subject to the sort memory restrictions.
  • If N is less than 5% of the total documents in the collection,

Warning

$sample may output the same document more than once in its result set. For more information, see Cursor Isolation.

Example

Given a collection named users with the following documents:

{ "_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:

db.users.aggregate(
   [ { $sample: { size: 3 } } ]
)

The operation returns three random documents.

Was this page helpful?

Yes No

Thank you for your feedback!

We're sorry! You can Report a Problem to help us improve this page.