简体   繁体   English

在RethinkDB中使用lambda的多个过滤器

[英]Multiple filters with a lambda in RethinkDB

I'd like someone to confirm the correct way to create the below query. 我希望有人确认创建以下查询的正确方法。 The docs are full of trivial examples of queries, but some medium-complexity examples would be useful - in order to know best practices. 这些文档充满了简单的查询示例,但是一些中等复杂性的示例会很有用 - 以便了解最佳实践。

I can filter with a query such as this: 我可以使用以下查询进行过滤:

r.table('backups').filter(
    {'verified': True}
).run(conn)

The same query can be written this way, with ReQL lambda shorthand: 可以用这种方式编写相同的查询,使用ReQL lambda简写:

r.table('backups').filter(
    r.row['verified'] == True
).run(conn)

I then tried to add another filter to this query, like so, but it didn't return the correct results: 然后我尝试为此查询添加另一个过滤器,就像这样,但它没有返回正确的结果:

r.table('backups').filter(
    r.row['verified'] == True and r.row['id'].match("^aad")
).run(conn)

Is the correct way to write this query to use two filter calls? 编写此查询以使用两个filter调用的正确方法是什么?

r.table('backups').filter(
    r.row['verified'] == True
).filter(
    r.row['id'].match("^aad")
).run(conn)

Python's and operator does not translate into the RethinkDB query language. Python and运算符不会转换为RethinkDB查询语言。 You must use & instead: 您必须使用&而不是:

r.table('backups').filter(
  (r.row['verified'] == True) & r.row['id'].match("^aad")
).run(conn)

In fact this is the best approach: 实际上这是最好的方法:

r.table('backups').filter(
  (doc['verified'] == _filters.get('verified') if _filters.has_key("verified") else doc['verified']) & \
  (doc['id'] == _filters.get('id') if _filters.has_key("id") else doc['id'])
).run(conn)

Usually you have a dict of values that you want to filter and not mock values as the accepted answer. 通常,您有一个要过滤的值的字典,而不是模拟值作为接受的答案。 By default if some parameter is set null it will filter the null values. 默认情况下,如果某个参数设置为null,它将过滤空值。 This one doesnt do that, just filter the ones that are present in the dict 这个不会这样做,只过滤dict中存在的那些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM