【问题标题】:Getting 200 random fields from MongoDB从 MongoDB 获取 200 个随机字段
【发布时间】:2018-04-06 23:32:00
【问题描述】:

我在 MongoDB 中存储了 10 万条推文。每条推文的存储方式如下:

{
    "_id" : "123456789",
    "user_screenName " : "john doe",
    "text" : "some tweet"
}

我找到了http://bdadam.com/blog/finding-a-random-document-in-mongodb.htmlMongoDB: how to find 10 random document in a collection of 100?,但不确定这是否正是我需要的。

我想获得 200 个随机的 text 字段,以便进行分析。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以为此使用$sample 阶段。

    db.collection.aggregate({
        $sample: { size: 200 } // select 200 random documents
    }, {
        $project: {
            "_id": 0, // exclude "_id"
            "text": 1 // include "text"
        }
    })
    

    另外,MongoDB Compass 提供了一些很好的分析现有数据的功能。

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 2017-09-13
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多