【发布时间】:2017-11-21 02:16:55
【问题描述】:
我有一组反馈评级(沟通、及时性和交付),所有这些都可以包含 1-5 个值。我需要计算有多少人在每个评分中评分为 5 星,然后是 4 星,然后是 3 星,最后是 1 星。
这是我的用户架构
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'
}]
});
反馈架构
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
}
});
到目前为止,我正在使用 $match、$unwind、$lookup 和 $count 尝试获取值但失败了。这是我正在运行的代码...
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);
})
})
使用这段代码,这是我得到的结果
[
{
"Communication_1": 1
}
]
所以我尝试通过实现多个 $match 来获取 Communication 的所有评级数字(这不起作用)
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);
})
})
但我得到了一个空洞的回应。所以我觉得我做错了。
任何帮助将不胜感激!谢谢!
**更新
我也试过这段代码。只是为了得到1和4的通信值而已。
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);
})
})
这就是我得到的结果
[
{
"_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
}
]
算了,但这不是我想要的,我想要的是对每个评分进行总计数
这是我想要的输出
{
"comm1" : 1,
"comm2" : 0,
"comm3" : 0,
"comm4" : 1,
"comm5" : 1
}
【问题讨论】:
标签: javascript node.js mongodb mongoose aggregation-framework