Given I have the following products:
{
"_id": "2666df80782596200fca49557d757870",
"_rev": "3-99382057f6c484526835f1042753ccf2",
"type": "product",
"name": "Shirt",
"hersteller": "oska",
"price": 11.15
}
{
"_id": "2666df80782596200fca49557d758e8c",
"_rev": "1-01cc88e69e5ff30f0d011fdf61fbedbc",
"type": "product",
"name": "Pullover",
"hersteller": "acme",
"price": 7.58
}
And an order
{
"_id": "2666df80782596200fca49557d758228",
"_rev": "2-0b4b3a8605893b60c962b8ae78f0b775",
"type": "order",
"orderDate": "01.01.2012",
"positions": [
"2666df80782596200fca49557d757870",
"2666df80782596200fca49557d758e8c"
]
}
I want to query all orders with _id, orderDate and sum of all positions prices. How do I approach this?
Example:
[
{ "_id": "2666df80782596200fca49557d758228", "orderDate": "01.01.2012", "total": 18.73 }
]
Edit:
This example is made up and I know that a order's total shouldn't change the product's prices change for historical orders. It's bad example for something where a true "join" would be necessary. Just imagine I gotta change the totals when a product's price changes. Hence, I don't want to simply denormalize the totals field.
It's feasible to "fix" the totals in the orders when a product changes. However, when I have a lot of different document types refering to the product, I need to change them type-by-type with some function.
Sean's answer put the idea into my head that I could fix denormalized fields with a changes listener, but I'd need one listener for every time some doc type is related to another - sounds like a lot of work.