We can do it using `ref` functionality within the schema definition. This will give us leverage to establish the relationship between these two different models.
Once, you have defined your schema that we need to reference i.e, Let’s say user and post model, so our schema goes like this:
const mongoose = require(“mongoose”)
Const { Schema } = mongoose
const user_schema = new Schema({
name : string,
email_address : string,
});
# Posting Schema for User’s model
const post_schema = new Schema({
headline: string,
body : string,
author: { type : Schema.Types.ObjectId, ref = “User”}); # Here we are referencing user model
# Working with the models
const User_Model = mongoose.model(“User”, user_schema);
const Post_Model = mongoose.model(“Post”, post_schema);