RethinkDB,使用地图和过滤器进行查询

RethinkDB, query with map and filter

我有 rethinkdb 'competition' table。在第二级,我得到了跑步者,每个跑步者都有一个结果列表:

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

我做 rethinkdb 查询:

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)

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

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

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