I have an object with a list inside which there is another list, and I need a pipeline with which I can increment the value inside the second sheet.
Object example:
{
"date":{"$date":"2022-0328T00:00:00.000Z"},
"department":"first",
"test_id":0,
"test":[{
"question_id":1,
"answers":[{
"answer_id":1,
"count":2
}]
}, {
"question_id":2,
"answers":[{
"answer_id":3,
"count":1
}]
}]
}
I tried to solve it this way, but it doesn't always work.
feedback_filt = {
'date': feedback.date,
'department': feedback.department,
'test_id': feedback.test_id
}
question_filt = feedback_filt | {"test.question_id": {"$ne": question_id}}
answer_filt = (feedback_filt | {"test.question_id": question_id} |
{"test.answers.answer_id": {"$ne": answers_id}})
arr_question_filt = {"q.question_id": question_id}
arr_answer_filt = {"a.answer_id": answers_id}
self.__collection.update_one(feedback_filt, {"$setOnInsert": feedback.__getdict__()}, upsert=True)
self.__collection.update_one(question_filt, {"$push": {
"test": FeedbackTest(question_id, []).__getdict__()}})
self.__collection.update_one(answer_filt, {"$push": {
"test.$.answers": FeedbackAnswer(answers_id, 0).__getdict__()}})
result = self.__collection.update_one(feedback_filt, {"$inc": {"test.$[q].answers.$[a].count": 1}},
array_filters=[arr_question_filt, arr_answer_filt], upsert=True)
return bool(result and result.modified_count)