How do I update a value within an array in Ravendb? I need to use Query

641 Views Asked by At

I have several documents. Below I will list just two. How do I update the value of the "Value field" that is below the "Name field: StartDate" in all documents?

Example: 10/10/2019 to 09/09/2019 01/01/2019 to 09/09/2019

Document 1:

{
    "IdLoginTwm": 4330,
    "Parametros": [
        {
            "Name": "UserName",
            "Value": "[email protected]"
        },
        {
            "Name ": "StartDate",
            "Value ": "10/10/2019"
        }
    ]
}  

Document 2:

{
    "IdLoginTwm": 4330,
    "Parametros": [
        {
            "Name": "UserName",
            "Value": "[email protected]"
        },
        {
            "Name ": "FirstName",
            "Value ": "diego"
        },  
        {
            "Name ": "StartDate",
            "Value ": "01/01/2019"
        }
    ]
}  

Thanks!!

1

There are 1 best solutions below

1
On

You can use the PatchByQueryOperation() for this.

https://ravendb.net/docs/article-page/4.2/Csharp/client-api/operations/patching/set-based#patching-how-to-perform-set-based-operations-on-documents

The patch script should be similar to below

from Logins
update {
    var res = this.Parametros.filter(x => x.Name == "StartDate")
    if (res.length > 0)
    {
        res.forEach(x => x.Value = 'NULL')
    }
}