Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda