CouchDB database model

I have two entities:

  • Company [companyId (PK)] and
  • 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 (eg: "_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,


    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.


    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.
  • 链接地址: http://www.djcxy.com/p/86424.html

    上一篇: 获取startmenu的截图

    下一篇: CouchDB数据库模型