Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend


This article showed you two methods for establishing relationships between documents in MongoDB.

The first method, data referencing, is very much like establishing a foreign-key relationship between two tables in a relational database. One document references another document through the use of an ID of the target document. One such example is an Address document that contains a foreign key to the ID of the User belonging to that address of the User document.

The second method is to embed data. You can easily embed one document within another document and so on, giving the capability for your document to represent a complex object’s data structure using JSON syntax.

The advantage of using embedded data instead of referencing data is that you reduce the number of atomic queries needed to match documents by embedding data.

The fewest number of matches needed improves performance. Referencing data between documents is more common and normalizes the database into a more modular design, but sometimes performance trumps normalization, and the need to embed data to improve performance is necessary.

When designing your data models, think long term and consider whether your database will need to be more performance-oriented or work with more of a design-oriented approach instead.

One of the big advantages of a document database is scalability through the use of embedding data. Another thing to keep in mind is how to best model your one-to-many document relationships that can be done with either referenced or embedded data.

  • + Share This
  • 🔖 Save To Your Account