0% found this document useful (0 votes)
17 views2 pages

Example: $sample $sample

The $sample stage in a MongoDB pipeline will use either a pseudo-random cursor or collection scan and random sort to select N random documents from a collection, depending on whether the collection size and N are below certain thresholds and if $sample is the first stage. $sample may output the same document more than once.

Uploaded by

Nana Trala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Example: $sample $sample

The $sample stage in a MongoDB pipeline will use either a pseudo-random cursor or collection scan and random sort to select N random documents from a collection, depending on whether the collection size and N are below certain thresholds and if $sample is the first stage. $sample may output the same document more than once.

Uploaded by

Nana Trala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

$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.

If all the following conditions are met, $sample uses a pseudo-random cursor to


select documents:

 $sample is the first stage of the pipeline


 N is less than 5% of the total documents in the collection
 The collection contains more than 100 documents

If any of the above conditions are NOT met, $sample performs a collection scan


followed by a random sort to select N documents. In this case, the $sample stage is
subject to the sort memory restrictions.

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:

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

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

The operation returns three random documents.

You might also like