Although MongoDB doesn't support joins, it supports the reference from a document to another document using a convention named DBRef. DBRef enables the reference from one document to another using a special field that contains the collection name and the document ObjectId
field. Mongoose implements a similar behavior for supporting document referral using the ObjectID
schema type and the use of the ref
property. It also supports the population of the parent document with the child document when querying the database.
To understand this better, let's say you create another schema for blog posts called PostSchema
. Because a user authors a blog post, PostSchema
will contain an author
field that will be populated by a User
model instance. So, a PostSchema
will look like what is shown in the following code snippet:
const PostSchema = new Schema({
title: {
type: String,
required: true
},
content: {
type: String,
required: true
},
author: {
...