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 !