title: Model Users - Group relationship in CouchDB
body: <p>I have group and user documents that I want to store in CouchDB, user can join multiple groups. One group can have thousands of users. Here are the documents I came up with:</p>
  
  <pre><code>Group: { "_id":"group1", "name":"abc", "type":"group" }
  Group: { "_id":"group2", "name":"abc", "type":"group" }
  User: { "_id":"user1", "login":"whatever", "groupIds": ["group1" , "group2"], "type":"user" }
  User: { "_id":"user2", "login":"whatever", "groupIds": ["group2"], "type":"user" }
  </code></pre>
  
  <p>I want to get groups by user id. It seems like the best thing I can do is define this view:</p>
  
  <pre><code>function(doc) {
    if (doc.type == "user")
    {
      emit(doc._id, doc.groupIds);
    }
  }
  </code></pre>
  
  <p>But in this case I need to get group ids in one request and then each group in separate request.
  Is there a way to write <code>groups by user id</code> view so that all groups were returned in one request to CouchDB? </p>