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:
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?
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. 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. This one doesnt do that, just filter the ones that are present in the dict
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With