Python 之 MongoDB 指定大小的随机数据

8/30/2021 python

从 mongodb 中从符合条件的数据中随机出指定大小的数据:

def db_random_data(type: int, size: int = 21):
    dbRes = collection.aggregate([
        {
            "$match": {
                "is_gzh_upload": {"$exists": False},
                "type": type
            }
        },
        {'$sample': {'size': size}}
    ])
    res = list(dbRes)
    return res
上次更新: 11/17/2023, 6:52:17 AM