我正在MongoDB中编写查询。我想对每条评论中的每个成绩进行计数,如果该成绩大于6,则增加Grades_Count和GradesMoreThan6_Count,如果不大于,则仅递增Grades_Count。我找不到可以在投影中使用的$ inc函数。 我感谢所有帮助。
cancel_demand
评论数组
{$project: {
_id: 1,
reviews: 1,
Grades_Count: NumberInt(0),
GradesMoreThan6_Count: {$map:
{input: "$reviews", as: "review", in:
{$switch:
{branches:[
{case: { $gt: [ "$$review.responsive", 6 ] }},then: Grades_Count++ AND GradesMoreThan6_Count++},
{case: { $gt: [ "$$review.helpful", 6 ] }},then: Grades_Count++ AND GradesMoreThan6_Count++},
{case: { $gt: [ "$$review.kind", 6 ] }},then: Grades_Count++ AND GradesMoreThan6_Count++}
],
default: Grades_Count++}}},