Relationships

While CouchDB has no built-in concept of relationships between documents, there are some techniques you can use to model relationships in your documents and views. The CouchDB Wiki page on Modeling Entity Relationships in CouchDB and Google’s Modeling Entity Relationships article were used as a reference for this section. If you’d like to compare the following examples to a relational database, please reference the column descriptions for the publisher (Table 5-1), book (Table 5-2), author (Table 5-3), and book_author (Table 5-4).

Table 5-1. Column descriptions from the publisher table

FieldTypeNullKeyDefaultExtra
publisher_idvarchar(255)NOPRINULL
namevarchar(255)YESNULL

Table 5-2. Column descriptions from the book table

FieldTypeNullKeyDefaultExtra
book_idvarchar(255)NOPRINULL
publisher_idvarchar(255)YESMULNULL
titlevarchar(255)YESNULL
subtitlevarchar(255)YESNULL
formatsset('Print','Ebook','Safari Books Online')YESNULL
releaseddateYESNULL
pagesint(10) unsignedYESNULL

Table 5-3. Column descriptions from the author table

FieldTypeNullKeyDefaultExtra
author_idvarchar(255)NOPRINULL
firstnamevarchar(255)YESNULL
lastnamevarchar(255)YESNULL

Table 5-4. Column descriptions from the book_author junction table

FieldTypeNullKeyDefaultExtra
book_idvarchar(255)NOPRINULL
author_idvarchar(255)NOPRINULL

Warning

The relational database design outlined here is intended to make it easy for you to translate between the equivalent SQL and CouchDB queries. This is not necessarily a recommended database design. ...

Get Writing and Querying MapReduce Views in CouchDB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.