如何在 couchdb 视图中使用 OR
我在rails和couchdb上使用ruby,但我
在sql中的couchdb中有问题视图我想要:
从用户中选择 *,其中用户名 = ... 或电子邮件 = ... 且状态 = ...
如何创建这样的视图?
I use ruby on rails and couchdb but I have problem view in couchdb
in sql I want:
select * from user where username = ... or email = ... and status = ...
How do I create view like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该查询是数据的联接。 (不,这不是一个实际的
JOIN
语句,但我的意思是,您提出了 3 个不同的问题,并将结果“连接”到答案中。)解决方案 1:只需多次查询。
假设您创建 2 个视图,值是
status
,键如下所示:by_username
by_email
保留地图(Java)或字典(Python)、哈希 (Perl)、对象 (Javascript) 或任何您的键/值数据结构。您想要保留一组匹配的用户,并将其添加到其中。
查询每个视图。如果状态是您需要的,请将用户名添加到您的集中。当所有查询完成后,您就会得到完整的答案。
哦不!但这太慢了!事实上,很多时候,解决方案“理论上”很慢,但实际上它们对于要求来说已经足够快了。您有一个 Rails 服务器。它可能具有对 CouchDB 的高速、低延迟 (LAN) 访问。 CouchDB 中的每个查询始终是高效的索引扫描。所以你提出了 3 个请求,嘭,嘭,嘭!客户端通过低速、高延迟连接(互联网)查询 Rails,它可能不会注意到。
或者,根据您的语言和技能,您可以同时(异步)执行这些查询,并且查询时间基本上会很快。
解决方案 2:秘密多键查询
CouchDB 实际上支持对视图的一些“OR”查询。有关详细信息,请参阅 CouchDB 视图选项。
您需要一个视图来存储所有信息。我建议使用数组键,
[status, key_type, key_value]
。例如,如果您有两个用户,Alice 和 Bob,则视图键将为:您将如何查询
status = "sleeping" 和 username = "X" 或 email = "Y"
?这变成了对视图的多个查询,每个查询都有不同的键:
幸运的是,您可以同时查询多个键。
CouchDB 将在一次响应中返回所有结果。
总结
第二个解决方案非常强大,但是您必须做大量工作才能获得正确的查询。而且这种技术不能总是满足任何 SQL 查询。 SQL 更强大,可以提出您能想到的任何问题。对于 CouchDB,你总是必须先教它做什么。那很不方便。但结果是,查询保证很快。
考虑到方案2的不便,我个人更喜欢方案1。可以在CouchDB中进行“join”吗?当然!联接就是这么简单。只需进行多次查询,直到获得所需的数据。如果您的应用程序需要经常“加入”,并且使用 CouchDB 很困难且存在错误,该怎么办?这是一个强有力的信号,表明 CouchDB 可能不适合您的应用程序。
That query is a join of your data. (No, it is not an actual
JOIN
statement, but I mean, you are asking 3 different questions and "joining" the results in the answer.)Solution 1: Just query multiple times.
Suppose you make 2 views, the value is the
status
and the keys are like this:by_username
by_email
Keep a map (Java), or dict (Python), hash (Perl), object (Javascript), or whatever your key/value data structure is. You want to keep a set of matching users, and add to it.
Query each view. If the status is what you are need, add the usernames to your set. When all queries are complete, you have the full answer.
Oh no! But that is so slow! In fact, very often, solutions are slow "in theory" but actually they are plenty fast for the requirements. You have a Rails server. It probably has high-speed, low-latency (LAN) access to CouchDB. Every query in CouchDB is always an efficient index scan. So you make 3 requests, bam, bam, bam! The client queries Rails over a low-speed, high-latency connection (the Internet), it probably will not notice.
Alternatively, depending on your language and skills, you can do these queries simultaneously (asynchronously) and the query time will, basically, be fast.
Solution 2: The secret multiple-keys query
CouchDB actually supports some "OR" queries to a view. See the CouchDB view options for details.
You need one view to store all of the information. I suggest array keys,
[status, key_type, key_value]
. For example, if you have two users, Alice and Bob, the view keys would be:How would you query for
status = "sleeping" and username = "X" or email = "Y"
?This becomes multiple queries to the view, each with a different key:
Fortunately, you can query for multiple keys at the same time.
CouchDB will return all of the results in one response.
Summary
The second solution is very powerful, however you must do a lot of work to get the right query. And this technique cannot always satisfy any SQL query. SQL is more powerful for asking any question you can think of. For CouchDB, you always have to teach it what to do first. That is inconvenient. But the result is, queries are guaranteed to be fast.
Considering the inconvenience of solution 2, I personally prefer solution 1. Can you do a "join" in CouchDB? Sure! Joins are so simple. Just make multiple queries until you have the data you need. What if your application needs to "join" very often, and using CouchDB is difficult and buggy? That is a powerful sign that CouchDB may not be appropriate for your application.