$ ADDFIELD与三个$ cond mongodb

发布于 2025-01-18 15:52:59 字数 3590 浏览 0 评论 0原文

我有来自聚合 $lookup 的输出数据

     [
      {
        _id: 1,
        name: "Abraham",
        class: "V",
        question_answered: [
          {
            id: "quest1",
            answer: "A",
            score: 10,
            question: {
              soal: "apa judul lagu?",
              correct_answer: "A",
              type_question: "Essay"
            }
          },
          {
            id: "quest2",
            answer: "C",
            score: null,
            question: {
              soal: "apa judul lagu B?",
              correct_answer: "B",
              type_question: "Essay"
            }
          },
          {
            id: "quest3",
            answer: "C",
            score: 10,
            question: {
              soal: "apa judul lagu C?",
              correct_answer: "C",
              type_question: "essay_pg"
            }
          },
          
        ]
      },
      {
        _id: 2,
        name: "Brenda",
        class: "V",
        question_answered: [
          {
            id: "quest1",
            answer: "A",
            score: 10,
            question: {
              soal: "apa judul lagu A?",
              correct_answer: "A",
              type_question: "Essay"
            }
          },
          {
            id: "quest2",
            answer: "C",
            score: 0,
            question: {
              soal: "apa judul lagu B?",
              correct_answer: "B",
              type_question: "Essay"
            }
          }
        ]
      }
    ]

,我需要在我通过一些条件 if、elseif、else 获得的每个数据中添加额外的字段 formated_status_evaluation_essayformated_status_evaluation_essay_pg 。我将给出示例 addfield 条件之一,或多或少像这样:

IF(question_answered.question.type_question == 'Essay' 并且没有分数 每个论文类型的问题都为空)然后, formated_status_evaluation_essay = "完整评分"。

ELSEIF(有论文类型问题且至少有一个空分) 然后,formated_status_evaluation_essay =“评分不完整”

ELSEIF(如果没有论文类型问题)那么, formated_status_evaluation_essay = "没有问题"

formated_status_evaluation_essay_pg 也是如此。我期望的输出是这样的。

    [
      {
        _id: 1,
        name: "Abraham",
        class: "V",
        question_answered: [....],
        formated_status_evaluation_essay: incomplete scoring,
        formated_status_evaluation_essay_pg: complete scoring,
    
      },
      {
        _id: 2,
        name: "Brenda",
        class: "V",
        question_answered: [....],
        formated_status_evaluation_essay: complete scoring,
        formated_status_evaluation_essay_pg: no question,
      }
    ]

关于输出的解释。 _id:1,评估论文不完整,因为它有一个对象包含空分数。但evaluation_essay_pg包含完整的 评分,因为 essay_pg 类型的所有内容都有一个分数。

_id:2,evaluation_essay 已完成,因为所有类型为essay 的问题都有分数。但 essay_pg 不包含问题,因为 Question_answer.question.type_question 中没有 essay_pg 类型。

我已经尝试过这个,但仍然对三个条件的编码感到困惑,就像我之前解释的那样。我将这样的代码放在 $lookup 聚合的末尾。

    { 
        '$addFields': { 
          'formated_status_evaluation_essay': { 
               '$cond': [ 
                   {
                    '$and': [ 
                      {'$$question_answer.question.type_soal ': 
                      'essay'},
                      {'$$question_answer.nilai':{$ne:null}},
                      ]    
                    },
                   'already scoring', 
                   'havent scoring' 
               ] 
            } 
        } 
    }  

我几乎得到了我的预期,但是我写的语法似乎仍然错误。如果你们能帮助我,我将非常感激。工作了两天了,还是没有得到答复。

I have this output data from aggregation $lookup

     [
      {
        _id: 1,
        name: "Abraham",
        class: "V",
        question_answered: [
          {
            id: "quest1",
            answer: "A",
            score: 10,
            question: {
              soal: "apa judul lagu?",
              correct_answer: "A",
              type_question: "Essay"
            }
          },
          {
            id: "quest2",
            answer: "C",
            score: null,
            question: {
              soal: "apa judul lagu B?",
              correct_answer: "B",
              type_question: "Essay"
            }
          },
          {
            id: "quest3",
            answer: "C",
            score: 10,
            question: {
              soal: "apa judul lagu C?",
              correct_answer: "C",
              type_question: "essay_pg"
            }
          },
          
        ]
      },
      {
        _id: 2,
        name: "Brenda",
        class: "V",
        question_answered: [
          {
            id: "quest1",
            answer: "A",
            score: 10,
            question: {
              soal: "apa judul lagu A?",
              correct_answer: "A",
              type_question: "Essay"
            }
          },
          {
            id: "quest2",
            answer: "C",
            score: 0,
            question: {
              soal: "apa judul lagu B?",
              correct_answer: "B",
              type_question: "Essay"
            }
          }
        ]
      }
    ]

I need to add additional field formated_status_evaluation_essay and formated_status_evaluation_essay_pg in each data that i get with some few condition if,elseif, else. i'll give one of example addfield condition, more or less like this one:

IF(question_answered.question.type_question == 'Essay' and no score is
null in every essay type question) then,
formated_status_evaluation_essay = "complete scoring".

ELSEIF(there's essay type question and have at least one null score)
then, formated_status_evaluation_essay = "Incomplete scoring"

ELSEIF(if theres no essay type question) then,
formated_status_evaluation_essay = "no question"

Same goes to formated_status_evaluation_essay_pg. The output that i expected is like this.

    [
      {
        _id: 1,
        name: "Abraham",
        class: "V",
        question_answered: [....],
        formated_status_evaluation_essay: incomplete scoring,
        formated_status_evaluation_essay_pg: complete scoring,
    
      },
      {
        _id: 2,
        name: "Brenda",
        class: "V",
        question_answered: [....],
        formated_status_evaluation_essay: complete scoring,
        formated_status_evaluation_essay_pg: no question,
      }
    ]

The explanation about the output.
_id:1, get evaluation_essay incomplete because it has one object that contain null score. But the evaluation_essay_pg contain complete
scoring because essay_pg type all of it have a score.

_id:2, evaluation_essay is complete because all question with type essay have a score. But essay_pg contain no question because theres no essay_pg type in question_answer.question.type_question.

I've tried this and still confuse to code three condition like i've explained before. I put code like this in the end of $lookup aggregation.

    { 
        '$addFields': { 
          'formated_status_evaluation_essay': { 
               '$cond': [ 
                   {
                    '$and': [ 
                      {'$question_answer.question.type_soal ': 
                      'essay'},
                      {'$question_answer.nilai':{$ne:null}},
                      ]    
                    },
                   'already scoring', 
                   'havent scoring' 
               ] 
            } 
        } 
    }  

i almost get what i expected but, seems still have a wrong syntax i wrote. I would be very thankfull if you guys can help me. Been working for two days still got no answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2025-01-25 15:52:59

尝试通过使用$ switch处理分支来使代码更具可读性。

db.collection.aggregate([
  {
    "$addFields": {
      "formated_status_evaluation_essay": {
        "$filter": {
          "input": "$question_answered",
          "as": "q",
          "cond": {
            $eq: [
              "$q.question.type_question",
              "Essay"
            ]
          }
        }
      },
      "formated_status_evaluation_essay_pg": {
        "$filter": {
          "input": "$question_answered",
          "as": "q",
          "cond": {
            $eq: [
              "$q.question.type_question",
              "essay_pg"
            ]
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "formated_status_evaluation_essay": {
        "$switch": {
          "branches": [
            {
              "case": {
                $and: [
                  {
                    "$allElementsTrue": [
                      {
                        "$map": {
                          "input": "$formated_status_evaluation_essay.score",
                          "as": "s",
                          "in": {
                            $ne: [
                              "$s",
                              null
                            ]
                          }
                        }
                      }
                    ]
                  },
                  {
                    $ne: [
                      {
                        $size: "$formated_status_evaluation_essay"
                      },
                      0
                    ]
                  }
                ]
              },
              "then": "complete scoring"
            },
            {
              "case": {
                "$anyElementTrue": [
                  {
                    "$map": {
                      "input": "$formated_status_evaluation_essay.score",
                      "as": "s",
                      "in": {
                        $eq: [
                          "$s",
                          null
                        ]
                      }
                    }
                  }
                ]
              },
              "then": "incomplete scoring"
            }
          ],
          default: "no question"
        }
      },
      "formated_status_evaluation_essay_pg": {
        "$switch": {
          "branches": [
            {
              "case": {
                $and: [
                  {
                    "$allElementsTrue": [
                      {
                        "$map": {
                          "input": "$formated_status_evaluation_essay_pg.score",
                          "as": "s",
                          "in": {
                            $ne: [
                              "$s",
                              null
                            ]
                          }
                        }
                      }
                    ]
                  },
                  {
                    $ne: [
                      {
                        $size: "$formated_status_evaluation_essay_pg"
                      },
                      0
                    ]
                  }
                ]
              },
              "then": "complete scoring"
            },
            {
              "case": {
                "$anyElementTrue": [
                  {
                    "$map": {
                      "input": "$formated_status_evaluation_essay_pg.score",
                      "as": "s",
                      "in": {
                        $eq: [
                          "$s",
                          null
                        ]
                      }
                    }
                  }
                ]
              },
              "then": "incomplete scoring"
            }
          ],
          default: "no question"
        }
      }
    }
  }
])

这是 mongo playground 供您参考。

Try to make the code a little bit more readable by using $switch to handle the branching.

db.collection.aggregate([
  {
    "$addFields": {
      "formated_status_evaluation_essay": {
        "$filter": {
          "input": "$question_answered",
          "as": "q",
          "cond": {
            $eq: [
              "$q.question.type_question",
              "Essay"
            ]
          }
        }
      },
      "formated_status_evaluation_essay_pg": {
        "$filter": {
          "input": "$question_answered",
          "as": "q",
          "cond": {
            $eq: [
              "$q.question.type_question",
              "essay_pg"
            ]
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "formated_status_evaluation_essay": {
        "$switch": {
          "branches": [
            {
              "case": {
                $and: [
                  {
                    "$allElementsTrue": [
                      {
                        "$map": {
                          "input": "$formated_status_evaluation_essay.score",
                          "as": "s",
                          "in": {
                            $ne: [
                              "$s",
                              null
                            ]
                          }
                        }
                      }
                    ]
                  },
                  {
                    $ne: [
                      {
                        $size: "$formated_status_evaluation_essay"
                      },
                      0
                    ]
                  }
                ]
              },
              "then": "complete scoring"
            },
            {
              "case": {
                "$anyElementTrue": [
                  {
                    "$map": {
                      "input": "$formated_status_evaluation_essay.score",
                      "as": "s",
                      "in": {
                        $eq: [
                          "$s",
                          null
                        ]
                      }
                    }
                  }
                ]
              },
              "then": "incomplete scoring"
            }
          ],
          default: "no question"
        }
      },
      "formated_status_evaluation_essay_pg": {
        "$switch": {
          "branches": [
            {
              "case": {
                $and: [
                  {
                    "$allElementsTrue": [
                      {
                        "$map": {
                          "input": "$formated_status_evaluation_essay_pg.score",
                          "as": "s",
                          "in": {
                            $ne: [
                              "$s",
                              null
                            ]
                          }
                        }
                      }
                    ]
                  },
                  {
                    $ne: [
                      {
                        $size: "$formated_status_evaluation_essay_pg"
                      },
                      0
                    ]
                  }
                ]
              },
              "then": "complete scoring"
            },
            {
              "case": {
                "$anyElementTrue": [
                  {
                    "$map": {
                      "input": "$formated_status_evaluation_essay_pg.score",
                      "as": "s",
                      "in": {
                        $eq: [
                          "$s",
                          null
                        ]
                      }
                    }
                  }
                ]
              },
              "then": "incomplete scoring"
            }
          ],
          default: "no question"
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文