//Here is the article
let articleSchema = new Schema({
title: String,
content: String
)}
//Here are the comments
let commentSchema = new Schema({
title: String,
content: String,
article: { type: Schema.Types.ObjectId, ref: 'Article' }
)}
var Article = mongoose.model('Article', articleSchema );
var Comment = mongoose.model('Comment', commentSchema );
When I want to get the list of articles again, I will get the comments under the article through the article _id. I have thought for a long time that “populate” can only look up the articles from the comments. what should I do? it is impossible to write ref with n comments in the article. It doesn’t feel realistic. Can we only get the list of articles and go through each article again? solve
Article can store a comment _id array, and then can populate comments.
let articleSchema = new Schema({ title: String, content: String, comments:[{ type: Schema.Types.ObjectId, ref: 'Comment' }] )}