MongoDB - Relationships

Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1: N, N: 1 or N: N.

Let us consider the case of storing addresses for users. So, one user can have multiple addresses making this a 1: N relationship.

This approach maintains all the related data in a single document which makes it easy to retrieve and maintain. The whole document can be retrieved in a single query like this:

>db.users.findOne({"name":"Tom Benzamin"},{"address":1})

Note that in the above query, db and users are the database and collection respectively.

The drawback is that if the embedded document keeps on growing too much in size, it can impact the read/write performance.

Modeling Referenced Relationships

This is the approach of designing normalized relationship. In this approach, both the user and address documents will be maintained separately but the user document will contain a field that will reference the address document's id field.

As shown above, the user document contains the array field address_ids which contains ObjectIds of corresponding addresses.Using these ObjectIds, we can query the address documents and get address details from there. With this approach, we will need two queries: first to fetch the address_ids fields from user document and second to fetch these addresses from address collection.