2

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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Ganesh Ram Ravi
  • 169
  • 1
  • 10

2 Answers2

2

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().

Embri
  • 622
  • 3
  • 15
  • I understand, but in some case if I want do so, what will be right option to do this ?. Say for same if I have 100 nested document in a parent document in this case what will be the right approach ? If I still want to load the full document and edit a piece of one nested document I feel it would be a complex approach. – Ganesh Ram Ravi Dec 10 '18 at 15:47
  • if you want to update "products/2-A" document and the denormalized data inside the order you only need to load and update these two documents, it's indifferent what you do in the document it is fully persisted each time the entity is edited. More in general if you need to update several documents (for example thousand of Orders) you could consider to use patching, but i think it isn't this case. – Embri Dec 11 '18 at 07:24
  • So which one you recommend ? Patch update or create a independent document and edit ? – Ganesh Ram Ravi Dec 11 '18 at 08:20
  • It's difficult without the knowladge of your context, it is not clear how you have structured the db and why. Personally until i have performance issues i would avoid patch – Embri Dec 11 '18 at 08:54
  • 2
    If you find yourself modifying an object inside another object often, and independently of the parent object, that is one of the signs that the embedded object should be an independent document. – Judah Gabriel Himango Dec 11 '18 at 18:51
  • Seems like I should not store the object as a nested document, Hence I will create a two independent document and apply joins using map-reduce. It seems feasible, your comments, please ? – Ganesh Ram Ravi Dec 13 '18 at 15:42
2

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

Tom
  • 836
  • 7
  • 14