RethinkDB:​​匹配字符串列表中的字符串

RethinkDB: matching a string from a list of strings

这是我在 rethinkDB 中的 table

[{"username": "row1", "some_key": ["str1", "str2"]}, {"username": "row2", "some_key": ["str3", "blah"]}, {"username": "row3", "some_key": ["blah", "blahblah"]}]

field(列)name可以重复。我有一个 list ['row1', 'row2'].

我想运行查询并获取list

中存在name的所有documents(行)

到目前为止我有这个:

r.db(self.PROJECT_DB).table(self.PROJECT_TABLE_PICWIZ).filter(r.row['username'] == name for name in following).limit(5).run(self.db_connection)

following就是这里的list

但是这 returns 所有 documents(行)

假设这是 Python。我认为根据我的理解,你想要这样的东西,考虑到 following 只是一个列表:

picwiz = r.db(self.PROJECT_DB).table(self.PROJECT_TABLE_PICWIZ)
picwiz.filter(lambda doc: r.expr(following).contains(doc['username']))\
  .limit(5)\
  .run(self.db_connection)

这是正在发生的事情:

此过滤器采用匿名函数,该函数采用名为 following 的字符串列表,然后针对 table 中的每个文档,通过 [=17] 检查用户名字段是否在该列表中=] 和 returns True 如果是,如果没有,则 False 从最终结果中添加或删除它。

也可能用以下内容替换 lambda 函数,以获得更多 Pythonic 和更少的 ReQL-ish。

lambda doc: doc['username'] in following