$addfield 在特定条件下 MongoDB
$addfield in spesific condiiton MongoDB
聚合得到的时候有这个数据
[{_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'
}
},
]
}
]
现在我想添加带条件的字段
添加名称的字段
formated_status_evaluation_essay 和 formated_status_evaluation_essay_选项,
有条件:
IF(question_answer.question.type_question === 'Essay' && score !== null)
formated_status_evaluation_essay = 'already scoring'
Elseif(question_answer.question.type_question === 'Essay' && score === null)
formated_status_evaluation_essay = 'haven't scoring'
Elseif(question_answer.question.type_question === 'Essay' is not exists
formated_status_evaluation_essay = 'No essay question'
formated_status_evaluation_essay_option 与 evaluation_essay
条件相同
Soo 预期的输出会像
[{_id: 1,
name:'Abraham',
class:'V',
question_answered:[.........]
formated_status_evaluation_essay:'havent Scoring',
formated_status_evaluation_essay_option:'Already Scoring'
}
]
如何使用 mongodb 中的条件编写正确的 addfield 以生成这样的输出?
谢谢,我试了很多方法还是没有答案。
我试过了,但似乎我输入了错误的语法
{
'$addFields': {
'formated_status_evaluation_essay': {
'$cond': [
{
'$and': [
{'$$question_answer.question.type_soal ':
'essay'},
{'$$question_answer.nilai':{$ne:null}},
]
},
'already scoring',
'havent scoring'
]
}
}
}
db.collection.aggregate([
{
$set: {
formated_status_evaluation_essay: {
$cond: [
{
$and: [
{
$in: [
"Essay",
"$question_answered.question.type_question"
]
},
{
$ne: [
"$question_answered.score",
null
]
}
]
},
"already scoring",
"havent scoring"
]
}
}
}
])
聚合得到的时候有这个数据
[{_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'
}
},
]
}
]
现在我想添加带条件的字段 添加名称的字段 formated_status_evaluation_essay 和 formated_status_evaluation_essay_选项,
有条件:
IF(question_answer.question.type_question === 'Essay' && score !== null)
formated_status_evaluation_essay = 'already scoring'
Elseif(question_answer.question.type_question === 'Essay' && score === null)
formated_status_evaluation_essay = 'haven't scoring'
Elseif(question_answer.question.type_question === 'Essay' is not exists
formated_status_evaluation_essay = 'No essay question'
formated_status_evaluation_essay_option 与 evaluation_essay
条件相同Soo 预期的输出会像
[{_id: 1,
name:'Abraham',
class:'V',
question_answered:[.........]
formated_status_evaluation_essay:'havent Scoring',
formated_status_evaluation_essay_option:'Already Scoring'
}
]
如何使用 mongodb 中的条件编写正确的 addfield 以生成这样的输出? 谢谢,我试了很多方法还是没有答案。
我试过了,但似乎我输入了错误的语法
{
'$addFields': {
'formated_status_evaluation_essay': {
'$cond': [
{
'$and': [
{'$$question_answer.question.type_soal ':
'essay'},
{'$$question_answer.nilai':{$ne:null}},
]
},
'already scoring',
'havent scoring'
]
}
}
}
db.collection.aggregate([
{
$set: {
formated_status_evaluation_essay: {
$cond: [
{
$and: [
{
$in: [
"Essay",
"$question_answered.question.type_question"
]
},
{
$ne: [
"$question_answered.score",
null
]
}
]
},
"already scoring",
"havent scoring"
]
}
}
}
])