Professional Documents
Culture Documents
Example: $sample $sample
Example: $sample $sample
the size of the collection, the size of N, and $sample’s position in the pipeline.
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 }
copy
copied
db.users.aggregate(
[ { $sample: { size: 3 } } ]
)