1

I currently have a Customers and a Products collection... Customers contains a list of Subscriptions and a subscription contains a ProductId. I'm trying to create an index that will allow me to select only the product id's that are NOT used in the subscriptions so that I can delete them.

I only managed so far to create an index to retrieve the productIds used in the subscriptions. I've looked into multiple map reduce examples and even some transformations but nothing seems to make sense.

Any ideas?

Thanks!

Bianca
  • 382
  • 4
  • 12

1 Answers1

2

Ok... so it finally hit me. This is what I did: I built 2 maps, one for the Products collection and one for the Customers collection and I selected the product id and created an additional property called Usage. Usage was set to 0 for the products in the Product collection and 1 for products in the Customer->Subscriptions. In the reduce, I filtered by the sum of usage to get only unused products (sum = 0). See bellow the code:

Maps:

from prod in docs.Products
select new
{ProductId = prod.ExternalId,
Usage = 0
}
from customer in docs.Customers
from docSubscriptionsItem in ((IEnumerable<dynamic>)customer.Subscriptions).DefaultIfEmpty()
select new { ProductId = docSubscriptionsItem.ProductId,
Usage=1}

Reduce:

from result in results
group result by result.ProductId
into g
let sum = g.Sum(p=>p.Usage)
where sum == 0
select new
{ProductId=g.Key, Usage = sum}

Hope this helps someone!

Bianca
  • 382
  • 4
  • 12
  • 1
    I would recommend removing the where clause from the reduce. Then you can leave where Usage==0 to be on the client side. This would then give an index that would provide you with additional information about your system instead of limiting it to 1 specific purpose. – Chris Marisic Aug 28 '14 at 18:16