来自 MongoDB 的随机记录 [英] Random record from MongoDB
问题描述
我希望从庞大的集合(1 亿条记录)中随机获取一条记录.
I am looking to get a random record from a huge collection (100 million records).
最快最有效的方法是什么?
What is the fastest and most efficient way to do so?
数据已经存在,并且没有可以生成随机数并获得随机行的字段.
The data is already there and there are no field in which I can generate a random number and obtain a random row.
推荐答案
从 MongoDB 3.2 版本开始,您可以使用 $sample
聚合管道操作符:
Starting with the 3.2 release of MongoDB, you can get N random docs from a collection using the $sample
aggregation pipeline operator:
// Get one random document from the mycoll collection.
db.mycoll.aggregate([{ $sample: { size: 1 } }])
如果要从集合的过滤子集中选择随机文档,请在管道中添加 $match
阶段:
If you want to select the random document(s) from a filtered subset of the collection, prepend a $match
stage to the pipeline:
// Get one random document matching {a: 10} from the mycoll collection.
db.mycoll.aggregate([
{ $match: { a: 10 } },
{ $sample: { size: 1 } }
])
如注释中所述,当size
大于1时,返回的文档样本中可能存在重复.
As noted in the comments, when size
is greater than 1, there may be duplicates in the returned document sample.
这篇关于来自 MongoDB 的随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!