Thank you for great article.
But I have a concern, what if I use my custom Id:string as a key, what will the type be rather than mongoose.Schema.Types.ObjectId?
I am a web developer who loves to code and help people. I chiefly use 𝗠𝗘𝗥𝗡 stack for development but also C++ for general problem solving. I like to discover and continue to improve myself !
There is a concept called "Virtuals". I came across it when I was thinking the same thing. I can only give you hint about it because I have not learned or applied them. In virtuals, you define the conditions for virtuals: 'localField' and 'foreignField'. Local field refers to the name of the field of your current Schema. Foreign field is the name of the field which you are using in other schema to refer to your current Schema. Then you use populate normally.
// modal = AuthorsconstauthorsSchema={name:String,article:String}// model = ArticlesconstarticlesSchema={title:String}authorsSchema.virtual("yahoo"// can be any name for your virtual, used as key to populate{ref:"Articles",localField:"article",// name of field in author's schemaforeignField:"title",})constAuthors=mongoose.model('Authors',authorsSchema);constArticles=mongoose.model('Articles',articlesSchema);// using populateAuthors.find({}).populate("yahoo")// name of virtual
There maybe some mistake in above example but hope it helps you understand the basics atleast.
Maybe once I get time to study, I will write about it as well. :)
I am a web developer who loves to code and help people. I chiefly use 𝗠𝗘𝗥𝗡 stack for development but also C++ for general problem solving. I like to discover and continue to improve myself !
I am a web developer who loves to code and help people. I chiefly use 𝗠𝗘𝗥𝗡 stack for development but also C++ for general problem solving. I like to discover and continue to improve myself !
Thanks, that's helpful. But the "yahoo" could be anything, when we call .populate("field name of Authors"). There are something still not clear. However, I get over it by modify data type of _id field.
I am a web developer who loves to code and help people. I chiefly use 𝗠𝗘𝗥𝗡 stack for development but also C++ for general problem solving. I like to discover and continue to improve myself !
Thank you for great article.
But I have a concern, what if I use my custom Id:string as a key, what will the type be rather than mongoose.Schema.Types.ObjectId?
There is a concept called "Virtuals". I came across it when I was thinking the same thing. I can only give you hint about it because I have not learned or applied them. In virtuals, you define the conditions for virtuals: 'localField' and 'foreignField'. Local field refers to the name of the field of your current Schema. Foreign field is the name of the field which you are using in other schema to refer to your current Schema. Then you use populate normally.
There maybe some mistake in above example but hope it helps you understand the basics atleast.
Maybe once I get time to study, I will write about it as well. :)
For anyone that wants more info, you can read more here. mongoosejs.com/docs/tutorials/virt...
Thanks @paras594 for the article, really helpful
Welcome and thanks for the reference !! You made the article more useful :)
Thank you for helpfull explaination. You have explained all in easy way and clearly. I wish you good luck!
I am glad you found it helpful. Thank you as well :)
You too Good Luck !!
Thanks, that's helpful. But the "yahoo" could be anything, when we call
.populate("field name of Authors")
. There are something still not clear. However, I get over it by modify data type of _id field.Good...things are not 100% clear to me as well...but I am sure after trying and testing we can understand it better :)