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

132
Vistas
How to update multiple documents efficiently based on the value of nested property?

Okay so I need to update documents based on the values of its nested property values. Suppose I have this document:

{
     "_id": "61e3050a465c380aec9f56a1",
     "name": "John Doe",
     "status": "pending",
     "payment": [
             {
                  "status": "completed"
             }, 
             {
                  "status": "completed"
             }
     ]
}

I need to find documents where all payment.status is completed and when documents are found, then I need to update the outer status to completed.

I can do this in multiple queries and filter out the ones which are completed but it makes a lot of queries based on the amount of documents which I think is not efficient performance wise. So what I was thinking is if there is a way to do it in one query, may be with Aggregate.

Here's my implementation:

let orders          = await Orders.find( {} ).populate( 'payments' );
let completedOrders = orders.filter( ( order ) => order.payments.every( ( payment ) => payment.status === 'completed' ) );

    if ( completedOrders.length !== 0 ) {
        for ( let order of completedOrders ) {
             await Orders.findByIdAndUpdate( order._id, { "status": "completed" } );
        }
    }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do an update with aggregation pipeline. Use $map to create an boolean array to indicate whether payment.status is "completed". Then use $allElementsTrue with $cond to conditionally update the outer status field.

db.collection.update({},
[
  {
    "$addFields": {
      "status": {
        "$cond": {
          "if": {
            "$allElementsTrue": {
              "$map": {
                "input": "$payment.status",
                "as": "s",
                "in": {
                  $eq: [
                    "$$s",
                    "completed"
                  ]
                }
              }
            }
          },
          "then": "completed",
          "else": "$status"
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

over 4 years ago · Santiago Trujillo Denunciar
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