1
所以,這裏是我正在Python 2.7中使用最新的RethinkDB(1.14)編寫的一些代碼中有趣的簡化代碼片段。我的問題是現在我需要添加另一個條件,並且有太多可能的組合。這可以在一個過濾器聲明中完成嗎?Rethinkdb,Python和Filter
query = r.table('messages').order_by(r.desc('created'))
if tag is not None and read is not None:
query = query.filter(lambda n: (n['user_id'] == user_id) &
(n['tags'].contains(tag)) &
(n['read'] == read))
elif read is not None:
query = query.filter(lambda n: (n['user_id'] == user_id) &
(n['read'] == read))
elif tag is not None:
query = query.filter(lambda n: (n['user_id'] == user_id) &
(n['tags'].contains(tag)))
else:
query = query.filter(lambda n: n['user_id'] == user_id)
fields_list = query.skip(skip)\
.limit(limit)\
.run(g.db_conn)
作爲一個側面說明,這將是這麼容易得多,如果鏈接過濾器的工作,基本上是充當and
。但是現在看來,每個RethinkDB查詢只能有一個過濾器。
編輯:不知道之前發生了什麼,但鏈接過濾器確實有效。
鏈接過濾器應正常工作,使這個容易當你嘗試一下會發生什麼? – 2014-08-31 00:57:59
你是對的,我一定遇到了一個不同的問題,使它看起來像這樣。我已經更新了答案。謝謝! – heiskr 2014-08-31 04:28:21