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

195
Views
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" } );
        }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
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!