【发布时间】:2020-12-30 16:45:15
【问题描述】:
我一直在使用调查集合和 $lookup 来查找调查问题的相关答案。
现在我想介绍一个 user_oid 并使用 $addField 创建一个“user_answer_count”。
代码:
Survey.aggregate([
{ $match: { team_oid: mongoose.Types.ObjectId(req.params.t_oid) } },
{ $addFields: { count_questions: { $size: "$questions"} } },
{ $unwind: { path: "$questions" } },
{ $lookup: { from: 'answers', localField: 'questions', foreignField: 'question_oid', as: 'answer' } },
{ $unwind: { path: "$answer", preserveNullAndEmptyArrays: true } },
{ $addFields: {
answer_count: { $cond: [{ $eq: [{ $type: "$answer" }, "object" ]}, 1, 0] } },
user_answer_count: { $cond: [{ $eq: ["$answer.user_oid", mongoose.Types.ObjectId(req.params.u_oid) ]}, 1, 0] }
},
{ $lookup: { from: 'votes', localField: '_id', foreignField: 'surveyOid', as: 'votes' } },
{ $unwind: { path: "$votes", preserveNullAndEmptyArrays: true } },
{ $addFields: { vote_count: { $cond: [{ $eq: [{ $type: "$votes" }, "object"] }, 0.2, 0] } } },
{
$group: {
_id: "$_id",
huddle_number: { $first: "$huddle_number" },
count_questions: { $first: "$count_questions" },
count_answers: { $sum: "$answer_count" },
count_votes: { $sum: "$vote_count" },
count_user_answers: { $sum: "$user_answer_count" },
sefirot_state: { $first: "$sefirot_state" },
created_date: { $first: "$created_date" },
avg_positive: { $avg: "$answer.positive" },
avg_creative: { $avg: "$answer.creative" },
avg_focused: { $avg: "$answer.focused" },
avg_friendly: { $avg: "$answer.friendly" },
avg_aligned: { $avg: "$answer.aligned" },
avg_alert: { $avg: "$answer.alert" }
}
}
]).exec((err, doc) => {
这里一切正常,第一个 $addField "answer_count" 完美运行。但是第二个“user_answer_count”失败了。
控制台说: Arguments must be aggregate pipeline operators
这是我第一次尝试将 $lookup 与 $addFields 一起使用,欢迎提出建议!
编辑:
示例文档
**Survey**
_id : 5f5a372d9ea9981e7c5773cb
questions :
0 : 5f522b025dd8993e58283522
1 : 5f47a892db023557105e2be3
2 : 5f522a9d5dd8993e58283520
3 : 5f0e23ef2e0fcb3fe04a7314
4 : 5f522b285dd8993e58283523
created_date : 2020-09-10T14:24:45.439+00:00
team_oid : 5f44cc20c74f8a444851d9c2
huddle_number : 22
sefirot_state : "Aligned"
__v : 0
**Answer**
_id : 5f58bb04772a5943a0b54ec9
question : "What does "winning" look like in this team?"
question_oid : 5f522bc55dd8993e58283526
user_team_oid : 5f44cc20c74f8a444851d9c2
user_oid : 5f1ef3e8accff82f3cae3957
comment : "It means Cobra Kai Never Die and Only Get Better"
created_date : 2020-09-09T11:22:44.098+00:00
__v : 0
【问题讨论】:
-
谢谢@turivishal - 我试过这个,但得到了同样的错误......
标签: mongodb aggregate pipeline