RavenDB Update a nested document?

446 Views Asked by At

Below is the sample "Order" document which is stored in RavenDB. In case if i want to update only a single nested document eg. "Product": "products/2-A" I want to update this nested element alone, how to can I achieve this using Raven C# client ?

Sample JSON document:

{
    "Company": "companies/65-A",
    "Employee": "employees/1-A",
    "Freight": 8.53,
    "Lines": [
        {
            "Discount": 0.2,
            "PricePerUnit": 19,
            "Product": "products/2-A",
            "ProductName": "Chang",
            "Quantity": 24
        },
        {
            "Discount": 0,
            "PricePerUnit": 10,
            "Product": "products/3-A",
            "ProductName": "Aniseed Syrup",
            "Quantity": 4
        },
        {
            "Discount": 0,
            "PricePerUnit": 22,
            "Product": "products/4-A",
            "ProductName": "Chef Anton's Cajun Seasoning",
            "Quantity": 1
        }        
    ],
    "OrderedAt": "1998-05-06T00:00:00.0000000",
    "RequireAt": "1998-06-03T00:00:00.0000000",
    "ShipTo": {
        "City": "Albuquerque",
        "Country": "USA",
        "Line1": "2817 Milton Dr.",
        "Line2": null,
        "Location": {
            "Latitude": 35.1154322,
            "Longitude": -106.6710792
        },
        "PostalCode": "87110",
        "Region": "NM"
    },
    "ShipVia": "shippers/2-A",
    "ShippedAt": null,
    "@metadata": {
        "@collection": "Orders",
        "@flags": "HasRevisions"
    }
}

Please provide your suggestions. Thanks

2

There are 2 best solutions below

0
On

Sound like you are looking for Patch, take a look at this documentation.

https://ravendb.net/docs/article-page/4.1/csharp/client-api%2foperations%2fpatching%2fset-based

6
On

If you mean to update the related document "products/2-A" that it isn't the element inside "Lines" array the only thing to do is to load the product, edit and save changes.

If you mean to update the element inside "Lines" i suggest you to understand how RavenDB works (or more in general how document oriented databases work). An entity is fully persisted as a document. Then you only need to load and edit the element in the list and persist "Order" with session.SaveChanges().