Skip to content

Instantly share code, notes, and snippets.

@neumino
Last active December 16, 2015 14:29
Show Gist options
  • Save neumino/5449011 to your computer and use it in GitHub Desktop.
Save neumino/5449011 to your computer and use it in GitHub Desktop.
# Query 1
cursor = r.db('test').table('test').filter(lambda doc:
(doc['status'] == 'open') & doc['baseaddresses'].map(lambda address: address == 'sathis...@example.org').reduce(lambda acc, val: acc | val) ).order_by('date').run()
for doc in cursor:
print doc
# Query 2
cursor = r.db('test').table('test').filter(lambda doc: ((doc['status'] == 'open') | (doc['status'] == 'paused')) & doc['baseaddresses'].map(lambda address: address == 'sathis...@example.org').reduce(lambda acc, val: acc | val) ).order_by('date').run()
for doc in cursor:
print doc
# Bonus - If you want 3 results per page, each time you can your page, you increment start_page and it's going to return the next 3 results.
start_page = 0
cursor = r.db('test').table('test').filter(lambda doc: ((doc['status'] == 'open') | (doc['status'] == 'paused')) & doc['baseaddresses'].map(lambda address: address == 'sathis...@example.org').reduce(lambda acc, val: acc | val) ).order_by('date').skip(page_start*3).limit(3).run()
for doc in cursor:
print doc
@al3xandru
Copy link

You could also do for the array matching:

cursor = r.db('test').table('test').filter(lambda doc:
(doc['status'] == 'open') & (doc['baseaddresses'].filter(lambda address: address == 'sathis...@example.org').count() > 0) ).run()

As a side note, we're planning to simplify the array matching part (and we already have some very good proposals for how to do it, but it will take a bit before it will get implemented)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment