On this page本页内容
If all of the following conditions are true, 如果以下所有条件均为真,$sample uses a pseudo-random cursor to select the N documents:$sample使用伪随机游标选择N个文档:
$sampleNIf any of the previous conditions are false, 如果之前的任何条件为假,$sample:$sample:
N documents.N个文档。Random sorts are subject to the sort memory restrictions.随机排序受排序内存限制。
If you are using the:如果您正在使用:
$sample may return the same document more than once in the result set.$sample可能在结果集中多次返回同一文档。$sample does not return duplicate documents. $sample不返回重复文档。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.该操作返回三个随机文档。