how a populate works with-in populate in mongoDb?

I have created three modals for MongoDB

const mongoose = require('mongoose');
const schema =  mongoose.Schema
const addCategorySchema = schema({
   categoryName:{
       type:String,
       unique:true,
       require:true
   },
   category:{
    type:String,
    default:'compose'
   },
   folder:[{
       type:schema.Types.ObjectId,
       ref:'composeFolder'
   }],
   userId:{
       type:String 
   }
},
{ timestamps:true }
)
module.exports = mongoose.model('compose_category', addCategorySchema)

above modal is for category

const mongoose = require('mongoose');
const schema = mongoose.Schema
const composefolderSchema = schema({
    folderName: {
        type: String,
        unique: true,
        require: true
    },
    template: [{
        type: schema.Types.ObjectId,
        ref: 'sentOrscheduleEmail'
    }],
    userId: {
        type: String
    }
},
    { timestamps: true }
)
module.exports = mongoose.model('composeFolder', composefolderSchema)

above is for to store folder in categories

const mongoose = require('mongoose');
const schema = mongoose.Schema
const EmailSchema = schema({
   from: {
       type: String,
       require: true
   },
   to: {
       type: Array
   },
   title: {
       type: String,
       require: true
   },
   subject: {
       type: String,
       require: true
   },
   template: {
       type: String
   },    sent_date: {
       type: String,
   },
   sent_time: {
       type: String,
   },

   category: {
       type: String,
       default: ' '
   },
   email_type: {
       type: String,
       default: ' '
   },
   email_status: {
       type: Boolean,
   },
   email_auth_key: {
       type: String,
       require: true
   },
   userId: {
       type: String,
       require: true
   },
   folderId: {
       type: String,
       require: true
   },
   createdBy: {
       type: String
   },
   adminId: {
       type: String
   },
   templete_Id: {
       type: String
   },
   is_Favorite: {
       type: Boolean,
       default: false
   },
   is_Sent: {
       type: Boolean,
       default: false
   },
   attachments: {
       type: Array
   },
   days: {
       type: String
   },
   smartLists: {
       type: Array
   }
},
   { timestamps: true }
)

module.exports = mongoose.model('sentOrscheduleEmail', EmailSchema)

and this last one for storing all mails in folder
so I have created all this three models using mongoose .
i know how populate works in mongoDb
eg.

db
.find()
.populate({
path:<relational key name>
})

so this is how populated works , my problem is that i have three models and i want relation in between this three models using populate in mongoDb

>Solution :

You can deep populate.

Example.

<Model>.find().populate({ path: 'path', populate: { path: 'path in path 1' })

You can also multi populate.

Example

<Model>.find().populate({ path: 'path'}).populate({ path: 'path2' })

Leave a Reply