RethinkDB: multiple comparisons filtering

流过昼夜 提交于 2019-12-11 02:44:50

问题


By the docs, it seems that in order to filter all users that are 30 years old OR 40 years old, I can do this (with python):

r.table("users").filter((r.row["age"].eq(30)) | (r.row["age"].eq(40))).run(conn)

Say I have a list based on input / request: [12, 14, 18, 88, 33 ...], how do I filter all the users that are in the age of one of the elements in the list above by iterating it (and not doing it hard coded)?


回答1:


That's one way to do it

valid_ages = [12, 14, 18, 88, 33]

r.table("users").filter(lambda user:
    r.expr(valid_ages).contains(user["age"])
).run(connection)

If you were using an index and get_all, you could do

r.table("users").get_all(*valid_ages, index="age").run(connection)

(You need to create the index age before that)



来源:https://stackoverflow.com/questions/23900791/rethinkdb-multiple-comparisons-filtering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!