Shawn de Wet
Shawn de Wet

Reputation: 5976

Store reference to parent document instead of storing copy of document

In the classic case of Orders and OrderLines, each OrderLine has a reference to its parent Order. When I save my OrderLine to RavenDB, it saves a copy of the full Order record as a property of each OrderLine. How do I get it to save only the link to the Order instead? Like this:

{ 
...other orderline properties...
Order : "orders/123"
}

instead of this

     {
        ...other orderline properties...
        Order : {
    ...all properties for order 123...
        }
}

Upvotes: 3

Views: 227

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241535

Welcome to non-relational databases! Order lines don't get ids. They don't get put in their own documents. They are stored where it matters - with the Order!

public class OrderLine
{
    public string Product { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

public class Order
{
    public string Id { get; set; }
    public string CustomerId { get; set; }
    public List<OrderLine> Lines { get; set; }
    public decimal Total { get; set; }
}

Produces documents like:

orders/123
{
    "CustomerId": "customers/456",
    "Lines": [
        { "Product": "Foo", "Quantity": 2, "Price": 10.00 },
        { "Product": "Bar", "Quantity": 3, "Price": 20.00 },
        { "Product": "Baz", "Quantity": 4, "Price": 30.00 }
    ]
    "Total": 200.00
}

See the documentation for more.

Upvotes: 3

Related Questions