I have a collection of feedback ratings (Communication, Timeliness & Delivery), all of these can contain 1-5 value. What i need is to count how many people rated 5 star on each ratings, then 4, then 3 then up to 1.
This is my User Schema
var UserSchema = new mongoose.Schema({
username: String,
fullname: String,
email: {
type: String,
lowercase: true,
unique: true
},
password: String,
feedback: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Feedback'
}]
});
Feedback Schema
var FeedbackSchema = new mongoose.Schema({
postname: String,
userWhoSentFeedback: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
message: String,
feedbacktype: String,
thumbsup: Boolean,
rating: {
delivery: Number,
timeliness: Number,
communication: Number
}
});
So far i'm using $match, $unwind, $lookup and $count trying to get the value but failed. Here's the code im running...
router.get('/testagg', (req, res)=>{
User.aggregate([
{"$match":
{ "username": "user1"}
},
{
"$lookup": {
"from": "feedbacks",
"localField": "feedback",
"foreignField": "_id",
"as": "outputResult"
}
}, {"$unwind": "$outputResult"},
{"$match": {"outputResult.rating.communication": {$eq: 1}}},{"$count": 'Communication_1'},
{
"$project": {
outputResult: 1,
Communication_1: 1
}
}
], (err, user)=>{
console.log(user)
res.json(user);
})
})
with this code, this is the result i got
[
{
"Communication_1": 1
}
]
So i tried to get all the rating numbers for Communication by implementing multiple $match (Which doesn't work)
router.get('/testagg', (req, res)=>{
User.aggregate([
{"$match":
{ "username": "user1"}
},
{
"$lookup": {
"from": "feedbacks",
"localField": "feedback",
"foreignField": "_id",
"as": "outputResult"
}
}, {"$unwind": "$outputResult"},
{"$match": {"outputResult.rating.communication": {$eq: 1}}},{"$count": 'Communication_1'},
{"$match": {"outputResult.rating.communication": {$eq: 2}}},{"$count": 'Communication_2'},
{"$match": {"outputResult.rating.communication": {$eq: 3}}},{"$count": 'Communication_3'},
{"$match": {"outputResult.rating.communication": {$eq: 4}}},{"$count": 'Communication_4'},
{"$match": {"outputResult.rating.communication": {$eq: 5}}},{"$count": 'Communication_5'},
{
"$project": {
outputResult: 1,
Communication_1: 1,
Communication_2: 1,
Communication_3: 1,
Communication_4: 1,
Communication_5: 1
}
}
], (err, user)=>{
console.log(user)
res.json(user);
})
})
But i got an empty response. So i think i'm doing it wrong.
Any help would be appreciated! Thank you!
**Update
I also tried this code. Just to get the communication value of 1 and 4 only.
router.get('/testagg', (req, res)=>{
User.aggregate([
{"$match":
{ "username": "user1"}
},
{
"$lookup": {
"from": "feedbacks",
"localField": "feedback",
"foreignField": "_id",
"as": "outputResult"
}
}, {"$unwind": "$outputResult"}, {
"$project": {
"outputResult.rating": 1,
comm1: { $cond: [{$eq: ['$outputResult.rating.communication', 1]}, 1, 0]},
comm4: { $cond: [{$eq: ['$outputResult.rating.communication', 4]}, 1, 0]}
}
},
{ $group: {
_id: '$outputResult.rating',
total: { $sum: 1 },
comm1: { $sum: '$comm1'},
comm4: { $sum: '$comm4'}
}
}
], (err, user)=>{
console.log(user)
res.json(user);
})
})
And this is the result i get
[
{
"_id": {
"communication": 1,
"timeliness": 1,
"delivery": 1
},
"total": 1,
"comm1": 1,
"comm4": 0
},
{
"_id": {
"communication": 5,
"timeliness": 5,
"delivery": 5
},
"total": 1,
"comm1": 0,
"comm4": 0
},
{
"_id": {
"communication": 4,
"timeliness": 4,
"delivery": 5
},
"total": 1,
"comm1": 0,
"comm4": 1
}
]
Well it counts it but this is not the one i want, what i want is to have a total count of each every rating
This is the output that i want
{
"comm1" : 1,
"comm2" : 0,
"comm3" : 0,
"comm4" : 1,
"comm5" : 1
}