Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
MongoDB double list increment

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)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!