Random record from MongoDB

后端 未结 27 2453
栀梦
栀梦 2020-11-22 01:22

I am looking to get a random record from a huge (100 million record) mongodb.

What is the fastest and most efficient way to do so? The data is already t

27条回答
  •  轮回少年
    2020-11-22 01:48

    Using Python (pymongo), the aggregate function also works.

    collection.aggregate([{'$sample': {'size': sample_size }}])
    

    This approach is a lot faster than running a query for a random number (e.g. collection.find([random_int]). This is especially the case for large collections.

提交回复
热议问题