couchdb views tied between two databases?

橙三吉。 提交于 2019-12-07 03:10:14

问题


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?


回答1:


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') {
            # ...
        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/5422622/couchdb-views-tied-between-two-databases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!