Document database structures are very different from the usual relational model. Instead of storing every logical grouping of data in separate tables that reference each other, you tend to store hierarchical document structures that contain all of the data needed. There are times, however, where it does not make sense to do this. Sometimes you really do want to have a separate document collection that is referenced by another.
MongoDB provides partial support for this, by allowing an ObjectID field type, where you can specify an ID as part of the document structure. But MongoDB does not directly give us the rest of the features that we need for managing references to documents in other collections. Fortunately, Mongoose provides the last mile of code for us, with document references. It will manage the references, loading and saving the data appropriately, with only a few changes to our code.
In this episode, then, I'll show you how to set up the blog post that we have been working on, to use a document reference for a blog post author and author information. I'll show you how to save the document reference, and I'll show you how to pre-populate the document when loading from the database.