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

228
Views
MongoDB - Massive array update using array's content pre-update

I had a problem where I have to update a collection which looks like this :

{
  "_id": ObjectId("61d229d13f5f944815deb7ca"),
  "date": "2022-01-02",
  "infos": [
    {
      "expiration": "2022-06-17",
      "rate": 1.0
    },
    {
      "expiration": "2022-06-17"
    }
  ]
}

And I want it to be like this, wihch means in infos array, if there's a rate then create an object rate with type, type2 and the value that was in rate before And if it was null, then the value is null

{
  "_id": ObjectId("61d229d13f5f944815deb7ca"),
  "date": "2022-01-02",
  "infos": [
    {
      "expiration": "2022-06-17",
      "rate": {
        "type": null,
        "type2": null,
        "value": 1.0
      }
    },
    {
      "expiration": "2022-06-17",
       "rate": {
        "type": null,
        "type2": null,
        "value": null
      }
    }
  ]
}

I want to change every object like this, not using javascript since it's a 10 M collections and it's very slow (15H+) using a js script.

Here is the script i've written:

db.derivativeObservedPrice.update(
{"asOfDate": "2022-06-06"},
{$set: {
    "legs.$[].rate": { 
               "type" : null,
               "type2": null,
               "value": null // use rate pre-update value?
           }
   }
},           
{multi : true}
)

But I don't really know how to acces previous rate value.. Is there a way to do this ? I'm pretty sure there is but haven't found the answer yet.

Thanks in advance !

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!