开发者

Nested databases in CouchDB

It seems you are unable to nest databases in CouchDB. How do people work around this limitation? For example, assume I want to create a blogging engine where each domain has a separate database. Within each database I might want a Users database, an Orders database, etc. to contain the various user documents, order documents, and so forth.

The obvious way seems to be a flat structure where the database name demarcates the artificial boundary between database nesting levels with a hyphen:

myblog.com-users
myblog.com-posts
myblog.com-comments
anotherblog.com-users
anotherblog.com-posts
anotherblog.com-comments
...hundreds more...

Another solution would be to keep the lower-level databases and mark each document with the top-level 开发者_如何学Pythonvalue:

users database containing a document User1, with field instance="Test" or a field domain="myblog.com"


I think you're misusing the term database here. There is no reason you can't store the users, posts, and comments data in a single couchdb database. Your couchdb views can separate out the user documents from the posts documents, from the comments documents.

example map function for user documents in a couchdb database:

function(doc) {
  if (doc.type = 'user') { // only return user documents
     emit([doc.domain, doc.id], doc); // the returned docs will be sorted by domain
  }
}

see View Api for ways to restrict that views results by domain using startkey and endkey with view collation.


I think the best solution is to have one database per domain, each storing domain specific data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜