【问题标题】:Select random document with filter with pymongo?使用pymongo选择带有过滤器的随机文档?
【发布时间】:2018-05-05 06:37:21
【问题描述】:

found, that to select random document,我需要使用$sample命令:

// Get one random document from the mycoll collection.
db.mycoll.aggregate(
   { $sample: { size: 1 } }
)

但是如果我需要过滤文档然后随机抽取一个呢?

我正在处理尚未处理的文档

query = {'start_time': {'$exists': False}}
hp_entries = mongo.hyperparameters_collection.find(query)

我将如何处理随机数?

【问题讨论】:

    标签: mongodb python-3.x pymongo


    【解决方案1】:

    与任何其他聚合阶段一样,它从前一阶段获取输入。

    $sample 前面加上 $match 以过滤文档。例如:

    db.hyperparameters_collection.aggregate([
        { "$match": { "start_time": { "$exists": False } } },
        { "$sample": { "size": 1 } }
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2020-10-09
      • 2016-08-29
      • 1970-01-01
      • 2021-10-17
      • 2019-03-19
      相关资源
      最近更新 更多