I asked this question previously regarding SQL Server: Complicated SQL Query--finding items matching multiple different foreign keys
Basically, I need to be able to find products that match multiple criteria. I have a scenario where I need to find products that match each of multiple categories and are found in multiple invoices.
The solution was a rather complex set of unions, which amounts to counting the number times a product matched the criteria and filtering for items whose count matched the count of criteria.
; with data (ID, Count) as (
select pc.ProductID, count(*) from ProductCategories pc (nolock)
inner join @categoryIDs /*table valued param*/ c on c.ID = pc.CategoryID
union all
select ip.ProductID, count(*) from InvoiceProducts ip (nolock)
inner join @invoiceIDs i on i.ID = ip.InvoiceID
)
select d.ID from data d
group by d.ID
having sum(d.Count) = @matchcount
But now, I am considering a NoSQL provider. So my question is, how would I create an index function to match this kind of query in RavenDB (or some other NoSQL project)?