Modeling relationships
In the following sections, we will explain how we can translate relationships in RDBMS theory into MongoDB’s document collection hierarchy. We will also examine how we can model our data for text search in MongoDB.
One-to-one
Coming from the relational DB world, we identify objects by their relationships. A one-to-one relationship could be a person with an address. Modeling it in a relational database would most probably require two tables: a person and an address table with a person_id
foreign key in the address table, as shown in the following diagram:
Figure 2.2 – Foreign key used to model a one-to-one relationship in MongoDB
The perfect analogy in MongoDB would be two collections, Person
and Address
, as shown in the following code:
> db.Person.findOne() { "_id" : ObjectId("590a530e3e37d79acac26a41"), "name" : "alex" } > db.Address.findOne() { "_id" : ObjectId...