You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a parent schema (tours) and a child schema (reviews), Each tour has many user reviews. my problem is no matter what I do the reviews won't be populated when I query a specific tour as I want it to do
Reviews Schema has a fields called tour and writer:
Tours Schema is going to populate (virtual populate) all reviews related to it:
// Virtual Populate :
tourSchema.virtual("reviews", {
ref:"Review",
// specifing the names of the fields (the foreign field and local field) in order to cennect the data sets :
localField: "_id", // the field where we store the data of foreign key we have gotten .
foreignField: "tour", // the field where the tour's id is being stored .
count: true
});
also, I've included the schema options for all of my models:
{
toJSON: { virtuals: true }, // means I need the virual to be part of the output .
toObject: { virtuals: true }, // mean I want it to be outputed as an obj .
}
I've read Mongoose documentation many times it's the same as mine, couldn't figure out where is the problem in my implementation
I'm using:
"mongoose": "^7.3.4"
maybe the problem is caused by mongoDB by any chance ?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a parent schema (tours) and a child schema (reviews), Each tour has many user reviews. my problem is no matter what I do the reviews won't be populated when I query a specific tour as I want it to do
Reviews Schema has a fields called tour and writer:
Tours Schema is going to populate (virtual populate) all reviews related to it:
also, I've included the schema options for all of my models:
here I populate the reviews :
I've read Mongoose documentation many times it's the same as mine, couldn't figure out where is the problem in my implementation
I'm using:
"mongoose": "^7.3.4"
maybe the problem is caused by mongoDB by any chance ?
Beta Was this translation helpful? Give feedback.
All reactions