How to do sub selects in couchdb, or, how to match a superset key to a subset using group_level
I'm new to both CouchDB and writing Map/Reduce views and I have a pretty complex question that hopefully has a not too complex answer to someone who's not just learning map/reduce and couchdb for the first time. I am working on a system that serves a json manifest to a client to configure itself with content that updates daily. On first run the clients register themselves with a few descriptive tags (say: screen size, OS, location), and the server returns back a group_id. The client uses that id to request its manifest every day. On the backend we arbitrarily group clients together that share certain tags to cut down on the number of unique manifests we need to store/serve. Our sales/admin person has a webapp where he can setup audiences to target specific content at specific groups. An audience can overlap multiple groups. The trick is, when the client reports in to get a fresh manifest we need to figure out which audience is the best fit to that client's group. The best matching audience will be the first audience who's tags are a subset of the submitted groups tags, e.g.: audience1: tagA, tagB, tagC, tagD audience2: tagA, tagC group1: tagA, tagB, tagC This group should match audience2, not audience1. If we were using an audiences tags to find the best group match (in other words, if group.tags were a subset of audience.tags) I could build a really effecient index like so: [tagA, tagB, tagC], group1._id [tagA, tagC, tagB], group1._id [tagB, tagA, tagC], group1._id [tagB, tagC, tagA], group1._id [tagC, tagA, tagB], group1._id [tagC, tagB, tagA], group1._id and use group_level=2 with key=[tagA, tagC] to match audience2 against the second line in the index. The problem is, I can't figure out how to do this going the other direction: matching a group.tags against an index of audience.tags, where the tags we know at query time (group.tags) are a superset of the tags we are trying to match against (audience.tags) I've got a firm grasp on simple m/r views, but I keep hiting dead ends on this one. Every solution I come to involves doing some sort of sub select in my view function, which doesn't work in couchdb views... any ideas on how I can attack a problem like this? Hopefully this decription makes some sense. I'm also open to hearing "you are approaching this totally wrong" :) Thanks, -Josh
