Robert Campbell
Robert Campbell

Reputation: 6958

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 value:

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

Upvotes: 3

Views: 2153

Answers (2)

Bernhard Gschwantner
Bernhard Gschwantner

Reputation: 1537

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

Upvotes: 3

Jeremy Wall
Jeremy Wall

Reputation: 25237

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.

Upvotes: 6

Related Questions