6

Say I have several databases on my couch instance (in my case a user account database and login session database) The sessions have fields corresponding to a field in the user database. Is there a way to create a view, or make a call that encompasses this correlation, or would it just have to be done with an external script? To be more clear, here's an example.

Account db:
{
   "_id": "78555fdfdd345debf427373f9dfaeca4",
...
   "username" : "bob"
}

Sessions db:

{
   "_id": "78555fdfdd345debf427373f9dfcd7ae",
..
   "accountId": "78555fdfdd345debf427373f9dfaeca4",
   "username": "bob"
}

Can I use emit or something like that to bundle together all this information in one call?

V_H
  • 1,793
  • 3
  • 34
  • 59

2 Answers2

5

No, however a common workaround is to have a "type" attribute for documents.

For example...

Application db:
{
   "_id": "account.78555fdfdd345debf427373f9dfaeca4",
   "type": "account",
...
   "username" : "bob"
}

{
   "_id": "session.78555fdfdd345debf427373f9dfcd7ae",
   "type": "session",
..
   "accountId": "account.78555fdfdd345debf427373f9dfaeca4",
   "username": "bob"
}

And then in your views:

map:
    function (doc) {
        if (doc.type=='account') {
            # ...
        }
    }
sausman
  • 253
  • 1
  • 8
  • Hi, thanks for the response. I'm confused about how this solves the problem. Is there a global view? I only know about views for specific dbs. – V_H Mar 25 '11 at 18:01
  • No, there isn't a global view for all databases. The solution above uses the same database for both accounts and sesssions. The way to differentiate between account and session documents is by giving each document a "type" attribute. You will also have to make sure the id's are different since they are in the same db now. This way you can access accounts and sessions in the same view. Because accounts and sessions are stored in the same db you have to add something similar to that last code snippet into map functions where you want to retrieve only accounts or only sessions in a view. – sausman Mar 25 '11 at 21:14
  • Ok, I see now. That's not possible since I don't have control over this DB but I see how it could be used in my own projects. Thanks! – V_H Mar 28 '11 at 20:27
3

No, unfortunately there is no way to connect data from multiple databases.

Each view is supposed to be self-contained, otherwise updating any document in 1 database will immediately need to trigger views indexes in every other database to be recalculated in all cases.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90