const PostSchema = new mongoose.Schema({
title: {
type: String,
},
text: {
type: String,
required: true,
}
postedBy: {
type: mongoose.Schema.ObjectId,
ref: "User",
}
likes: [{
type: mongoose.Schema.ObjectId,
ref: "User",
}, ],
likeLength: {
type: Number,
default: 0
}
});
let totalLikes = Post.aggregate([{
$match: {
postedBy: req.profile._id,
},
},
{
$group: {
_id: "$postedBy",
total_count: {
$sum: "$likeLength"
}
}
},
])
Volevo somma likeLength valori in tutti i Post collezioni filtrati dall'postedBy campo e di ottenere il riassunto risultato che tipo integer. Ho provato totalLikes funzione di cui sopra, ma non poteva fare.