1

I have two entities:

  1. Company [companyId (PK)] and
  2. Employee [employeeId (PK), companyId (PK, FK)].

Is there anyway I can organize my documents in CouchDB so that I can query an employee using the following format ($CouchDB is the URL to my CouchDB instance) ?

$CouchDB/company/{companyId}/employee/{employeeId}

I know that we can mix both Company and Employee documents in one database and use meta field (e.g: "_type") and map/reduce to query the document we want. There is nothing wrong with "map/reduce" approach, just want to double check so that I don't miss anything.

Many thanks,

2 Answers 2

1

You can not organize your documents that way. I agree, that is a very nice layout, however the short answer is that CouchDB does not support that.

Document IDs (and document URLs) are flat. They all share the same namespace. Another way of saying this is that the "_id" value in a document must be its primary key.

Sign up to request clarification or add additional context in comments.

Comments

0

Although with some quirks, document _id can contain /. Then company/COMPANYID/employee/EMPLOYEEID is a valid id.

I often use the following scheme to build the _id of a document (I prefer _):

EntityName_ENTITYID

With this _id:

  • I don't need _type field;
  • I get all companies with _all_docs?startkey="Company_"&endkey="Company_\fff0";
  • I get all employees within a company with _all_docs?startkey="Employee_COMPANYID_"&endkey="Employee_COMPANYID_\fff0";
  • I avoid id conflicts between different entities.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.