开发者

Query for set complement in CouchDB

开发者 https://www.devze.com 2023-03-12 06:20 出处:网络
I\'m not sure that there is a good way to do with with the facilities CouchDB provides, but I\'d like to somehow extract the relative complement of the sets of two different document types over a part

I'm not sure that there is a good way to do with with the facilities CouchDB provides, but I'd like to somehow extract the relative complement of the sets of two different document types over a particular key.

For example, let's say that I have documents representing users and posts, both of which have a (unique) username field. There's a validation in place ensuring that a user document exists for the username in every post, but there may be any number post documents with a given username, include none. It's trivial to create a view which counts the number of posts per username. The view can even include zero-counts by emitting zero post-counts for the user documents in the view map function. What I want to do t开发者_如何学运维hough is retrieve just the list of users who have zero associated posts.

It's possible to build the view I described above and filter client-side for zero-value results, but in my actual situation the number of results could be very, very large, and the interesting results a relatively small proportion of the total. Is there a way to do this sever-side and retrieve back just the interesting results?


I would write a map function to iterate through the documents and emit the users (or just usersnames) with 0 posts.

Then I would write a list function to iterate through the map function results and format them however you want (JSON, csv, etc).

(I would NOT use a reduce function to format the results, even if a reduce function appears to work OK in development. That is just my own experience from lessons learned the hard way.)


Personally I would filter on the client-side until I had performance issues. Next I would probably use Teddy's _filter technique—all pretty standard CouchDB stuff.

However, I stumbled across (IMO) an elegant way to find set complements. I described it when exploring how to find documents missing a field.

The basic idea

Finding non-members of your view obviously can't be done with a simple query (and a straightforward index scan.) However, it can be done in constant memory, and linear time, by simultaneously iterating through two query results at the same time.

One query is for all possible document ids. The other query is for matching documents (those you don't want). Importantly, CouchDB sorts query results, therefore you can calculate the complement efficiently.

See my details in the previous question. The basic idea is you iterate through both (sorted) lists simultaneously and when you say "hey, this document id is listed in the full set but it's missing in the sub-set, that is a hit.

(You don't have to query _all_docs, you just need two queries to CouchDB: one returning all possible values, and the other returning values not to be counted.)

0

精彩评论

暂无评论...
验证码 换一张
取 消