MongoDB聚合,$addFields不在$cond上工作
我一直在使用调查收集和$lookup来查找调查问题的相关答案 现在我想引入一个user\u oid并使用$addField创建一个“user\u answer\u count” 代码:MongoDB聚合,$addFields不在$cond上工作,mongodb,aggregate,pipeline,Mongodb,Aggregate,Pipeline,我一直在使用调查收集和$lookup来查找调查问题的相关答案 现在我想引入一个user\u oid并使用$addField创建一个“user\u answer\u count” 代码: Survey.aggregate([ { $match: { team_oid: mongoose.Types.ObjectId(req.params.t_oid) } }, { $addFields: { count_questions: { $size: "$questions&qu
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\u count”非常有效。但是第二个“用户回答计数”却失败了
控制台显示:参数必须是聚合管道运算符
这是我第一次尝试将$lookup与$addFields结合使用,因此,非常感谢您的建议
编辑:
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) => {
示例文档
**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
有一些修复,缺少括号和
$cond
语法不正确,而且这种方法获取数组计数是错误的
- 计数数组中的元素数使用
,用于$size
回答\u计数
- 您可以使用
数组运算符按用户获取计数,无需使用$reduce
应答数组$unwind
谢谢@turivishal-我试过了,但也犯了同样的错误…谢谢@turivishal。这是一个有用的答案,它工作得很好,但是当我应用到我的代码时,它不起作用。我想因为我在
aggregate
方法中执行之前的操作,所以数据已经结构化了?如果您想看一看,我在问题中添加了其余的聚合。我正在努力解决这个问题。再次感谢,如果我能完成这项工作,我会接受这个答案。好的,为操场上的其他收藏提供一些文件,所以我会调查它