【问题标题】:RethinkDB, query with map and filterRethinkDB,使用地图和过滤器进行查询
【发布时间】:2016-06-15 14:02:12
【问题描述】:

我已经重新思考数据库“竞赛”表。在第二级,我得到了跑步者,每个跑步者都有一个结果列表:

"total_results": {
  "433915": [  #runner_id
    {
      "judge": "15561671",
      "result": 5,
      "round": "1"
    },
    {
      "judge": "08136a59",
      "result": 4,
      "round": "1"
    }
  ]
}

我重新思考数据库查询:

results = (r.table('competitions').filter(lambda c: c['unique_id'] == competition_id)
    .map(lambda c: c['total_results'])
    .map(lambda t: t[runner_id])
.run(conn))

这段代码运行良好。现在我想根据“round”值应用过滤器。我在第二个 .map() 之后添加它,因此结果查询如下所示:

results = (r.table('competitions').filter(lambda c: c['unique_id'] == competition_id)
    .map(lambda c: c['total_results'])
    .map(lambda t: t[runner_id])
    .filter(lambda x: x['round'] == round)
.run(conn))

但我得到的是空列表。这对我来说很奇怪,因为如果我将 .filter() 移到 rethinkdb 查询之外并这样做:

by_round = filter(lambda x: x['round'] == round, results)

我得到了结果。 重新考虑查询时我应该做错了什么......你能给我一个提示吗?

附言我在数据库中有数千个结果。根据我的查询参数应该有几十个结果。

【问题讨论】:

    标签: python rethinkdb


    【解决方案1】:

    我认为您希望您的第二个 map 成为 concat_map。 (或者,如果您喜欢现有的输出格式,可以将filter 放在map 中,例如.map(lambda x: x.filter(...))。)

    【讨论】:

    • 嗨@mlucy,非常感谢您的回复!是的,你是对的,用 concat_map 替换我的第二张地图就可以了。您能否在答案中将 concatMap 替换为 concat_map (python 方式)?我会将您的回答标记为“已接受”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2012-08-22
    相关资源
    最近更新 更多