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
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
publisher_id | varchar(255) | NO | PRI | NULL | |
name | varchar(255) | YES | NULL |
Table 5-2. Column descriptions from the book table
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
book_id | varchar(255) | NO | PRI | NULL | |
publisher_id | varchar(255) | YES | MUL | NULL | |
title | varchar(255) | YES | NULL | ||
subtitle | varchar(255) | YES | NULL | ||
formats | set('Print','Ebook','Safari Books
Online') | YES | NULL | ||
released | date | YES | NULL | ||
pages | int(10) unsigned | YES | NULL |
Table 5-3. Column descriptions from the author table
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
author_id | varchar(255) | NO | PRI | NULL | |
firstname | varchar(255) | YES | NULL | ||
lastname | varchar(255) | YES | NULL |
Table 5-4. Column descriptions from the book_author junction table
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
book_id | varchar(255) | NO | PRI | NULL | |
author_id | varchar(255) | NO | PRI | NULL |
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.