我正在 Pymongo 聚合中执行管道,但集合没有显示任何更改



Dears,

这是我的管道,它以merge结束,它在Mongo指南针上运行得很顺利,但当它使用PyMongo执行时,它不起的作用

migrate_students_documents = [
{
'$set': {
'science_sections_act': [
'Science'
],
'english_sections_act': [
'Section 1: English Sitting 1', 'Section 2: English Sitting 2'
],
'math_sections_act': [
'Section 4: Math Sitting 1', 'Section 5: Math Sitting 2'
],
'reading_sections_act': [
'Section 3: Reading'
]
}
}, {
'$match': {}
}, {
'$unwind': {
'path': '$sections'
}
}, {
'$unwind': {
'path': '$scaled_scores_act'
}
}, {
'$group': {
'_id': [
{
'oid': '$_id'
}, {
'course_id': '$sections.course_id'
}
],
'sections': {
'$push': '$sections'
},
'scaled_scores_act_placement': {
'$mergeObjects': {
'$cond': {
'if': {
'$eq': [
'$scaled_scores_act.session_type', 0
]
},
'then': {
'english': '$scaled_scores_act.english',
'math': '$scaled_scores_act.math',
'reading': '$scaled_scores_act.reading',
'science': '$scaled_scores_act.science'
},
'else': '$$REMOVE'
}
}
},
'scaled_scores_act_exit': {
'$mergeObjects': {
'$cond': {
'if': {
'$eq': [
'$scaled_scores_act.session_type', 2
]
},
'then': {
'english': '$scaled_scores_act.english',
'math': '$scaled_scores_act.math',
'reading': '$scaled_scores_act.reading',
'science': '$scaled_scores_act.science'
},
'else': '$$REMOVE'
}
}
},
'science_answered_count_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$science_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.answered_count',
'else': 0
}
}
},
'english_answered_count_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$english_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.answered_count',
'else': 0
}
}
},
'math_answered_count_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$math_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.answered_count',
'else': 0
}
}
},
'reading_answered_count_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$reading_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.answered_count',
'else': 0
}
}
},
'science_total_questions_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$science_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.total_questions',
'else': 0
}
}
},
'english_total_questions_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$english_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.total_questions',
'else': 0
}
}
},
'math_total_questions_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$math_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.total_questions',
'else': 0
}
}
},
'reading_total_questions_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$reading_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.total_questions',
'else': 0
}
}
},
'science_correct_answers_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$science_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.correct_answers',
'else': 0
}
}
},
'english_correct_answers_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$english_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.correct_answers',
'else': 0
}
}
},
'math_correct_answers_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$math_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.correct_answers',
'else': 0
}
}
},
'reading_correct_answers_placement': {
'$sum': {
'$cond': {
'if': {
'$and': [
{
'$in': [
'$sections.section_name', '$reading_sections_act'
]
}, {
'$eq': [
'$sections.session_type', 0
]
}
]
},
'then': '$sections.correct_answers',
'else': 0
}
}
},
}
}, {
'$addFields': {
'stats': {
'district_id': {
'$first': '$sections.district_id'
},
'district_name': {
'$first': '$sections.district_name'
},
'course_id': {
'$first': '$sections.course_id'
},
'course_name': {
'$first': '$sections.course_name'
},
'course_curriculum': {
'$first': '$sections.course_curriculum'
},
'placement': {
'section_score': {
'english': {
'answered_count': '$english_answered_count_placement',
'total_questions': '$english_total_questions_placement',
'correct_answers': '$english_correct_answers_placement',
'percent_of_completion': {
'$cond': [
{
'$eq': [
'$english_total_questions_placement', 0
]
}, 0, {
'$divide': [
'$english_answered_count_placement', '$english_total_questions_placement'
]
}
]
}
},
'math': {
'answered_count': '$math_answered_count_placement',
'total_questions': '$math_total_questions_placement',
'correct_answers': '$math_correct_answers_placement',
'percent_of_completion': {
'$cond': [
{
'$eq': [
'$math_total_questions_placement', 0
]
}, 0, {
'$divide': [
'$math_answered_count_placement', '$math_total_questions_placement'
]
}
]
}
},
'reading': {
'answered_count': '$reading_answered_count_placement',
'total_questions': '$reading_total_questions_placement',
'correct_answers': '$reading_correct_answers_placement',
'percent_of_completion': {
'$cond': [
{
'$eq': [
'$reading_total_questions_placement', 0
]
}, 0, {
'$divide': [
'$reading_answered_count_placement', '$reading_total_questions_placement'
]
}
]
}
},
'science': {
'answered_count': '$science_answered_count_placement',
'total_questions': '$science_total_questions_placement',
'correct_answers': '$science_correct_answers_placement',
'percent_of_completion': {
'$cond': [
{
'$eq': [
'$science_total_questions_placement', 0
]
}, 0, {
'$divide': [
'$science_answered_count_placement', '$science_total_questions_placement'
]
}
]
}
}
},
'scaled_score': '$scaled_scores_act_placement'
},
}
}
}, {
'$group': {
'_id': {
'$first': '$_id.oid'
},
'stats': {
'$addToSet': '$stats'
}
}
}, {
'$project': {
'id': 1,
'stats': 1
}
}, 
{
'$merge': {
'into': 'students',
'on': '_id'
}
}
]

Pymongo在执行mongo.schalktalk.command时不会显示错误('aggregate','students',pipeline=migrate_students_documents,explaine=True(,但集合上没有任何更改

不要使用命令进行聚合,而是将下面的命令用于相同的管道

students = chalktalk.students.aggregate(migrate_students_documents)