Professional Documents
Culture Documents
Entity Relationships in A Document Database: Mapreduce Views For SQL Users
Entity Relationships in A Document Database: Mapreduce Views For SQL Users
Entity Relationships in A Document Database: Mapreduce Views For SQL Users
a Document Database
MapReduce Views for SQL Users
When to Choose a
Document Database
You’re using a relational database, but have been relying
heavily on denormalization to optimize read performance
"How fucked is my NoSQL database?" howfuckedismydatabase.com. 2009. http://howfuckedismydatabase.com/nosql/ (24 October 2012).
Entity
Relationship
Model
Join vs. Collation
SQL Query Joining
Publishers and Books
SELECT
`publisher`.`id`,
`publisher`.`name`,
`book`.`title`
FROM `publisher`
FULL OUTER JOIN `book`
ON `publisher`.`id` = `book`.`publisher_id`
ORDER BY
`publisher`.`id`,
`book`.`title`;
Joined Result Set
Publisher (“left”) Book “right”
Neither the “left” nor “right” side of the relationship contain any
direct references to each other
1. http://docs.doctrine-project.org/projects/doctrine-couchdb/
Features
Includes a CouchDB client library and ODM
/** @Document */
class BlogPost
{
/** @Id */
private $id;
/** @Field(type="string") */
private $headline;
/** @Field(type="string") */
private $text;
/** @Field(type="datetime") */
private $publishDate;
// getter/setter here
}
1. http://docs.doctrine-project.org/projects/doctrine-couchdb/en/latest/reference/introduction.html#architecture
Persisting an Entity[1]
1. http://docs.doctrine-project.org/projects/doctrine-couchdb/en/latest/reference/introduction.html#architecture
Querying an Entity[1]
1. http://docs.doctrine-project.org/projects/doctrine-couchdb/en/latest/reference/introduction.html#querying
Doctrine MongoDB ODM [1]
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/
Features
Maps documents using Doctrine’s persistence semantics
/** @MappedSuperclass */
abstract class BaseEmployee
{
/** @Id */
private $id;
/** @EmbedOne(targetDocument="Address") */
private $address;
// ...
}
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Defining Entities[1]
/** @Document */
class Employee extends BaseEmployee
{
/** @ReferenceOne(targetDocument="Documents
\Manager") */
private $manager;
// ...
}
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Defining Entities[1]
/** @Document */
class Manager extends BaseEmployee
{
/** @ReferenceMany
(targetDocument="Documents\Project") */
private $projects = array();
// ...
}
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Defining Entities[1]
/** @EmbeddedDocument */
class Address
{
/** @String */
private $address;
/** @String */
private $city;
/** @String */
private $state;
/** @String */
private $zipcode;
// ...
}
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Defining Entities[1]
/** @Document */
class Project
{
/** @Id */
private $id;
/** @String */
private $name;
public function __construct($name)
{
$this->name = $name;
}
// ...
}
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Persisting Entities[1]
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Persisting Entities[1]
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Persisting Entities[1]
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Persisting Entities[1]
1. http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/introduction.html#features-overview
Querying an Entity
// $dm is an instance of Doctrine\ODM\MongoDB
\DocumentManager
$manager = $dm->find("Documents\Manager",
$theID);
Final Thoughts
Document Databases Compared
to Relational Databases
Document databases have no tables (and therefore no columns)
One to Many ✓ ✓
Many to Many ✓ ✓
<= N* Relations ✓ ✓
> N* Relations ✓ ✓