Answer the question
In order to leave comments, you need to log in
How to update data using aggregate?
There is this code:
reviewSchema.statics.getAverageRating = async function(card) {
console.log(card);
const obj = await this.aggregate([
{
$match: { card: card }
},
{
$group: {
_id: "$card",
averageRating: { $avg: "$rating" },
ratingParams: { $avg: "$ratingParams" }
}
}
]);
try {
await this.model("cards").findByIdAndUpdate(card, {
averageRating: obj[0].averageRating,
ratingParams: obj[0].ratingParams
});
} catch (err) {
console.log(err);
}
};
reviewSchema.statics.getAverageRating = async function(card) {
console.log(card);
const obj = await this.aggregate([
{
$match: { card: card }
},
{
$group: {
_id: "$card",
averageRating: { $avg: "$rating" }
}
}
]);
try {
await this.model("cards").findByIdAndUpdate(card, {
averageRating: obj[0].averageRating
});
} catch (err) {
console.log(err);
}
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question