RethinkDB:​​多重比较过滤 [英] RethinkDB: multiple comparisons filtering

查看:48
本文介绍了RethinkDB:​​多重比较过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,似乎为了过滤所有 30 岁或 40 岁的用户,我可以这样做(使用 python):

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)

假设我有一个基于输入/请求的列表:[12, 14, 18, 88, 33 ...],我如何过滤上面列表中元素之一年龄的所有用户通过迭代它(而不是硬编码)?

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)?

推荐答案

这是一种方法

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

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

如果你使用索引和get_all,你可以这样做

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)

这篇关于RethinkDB:​​多重比较过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆