Hi all,
I am trying to use a map/reduce combination to be able to perform a partial bulk delete on a bucket.
Here is a sample of a document, :
"product-Walmart-NewYork-123": { sku: 123 name: "Coffee machine" price: $1.23 facility: "NewYork" }I want to delete all products available at the Walmart’s New York facility. I don’t know in advance all the skus of the products in this facility.
Solution 1 : delete in map
I can trick the system this way :
map = function (doc, meta) { if (meta.id.indexOf('product-Walmart-NewYork-') === 0) { delete(meta.id); } } reduce = nullBut this forces me to create a “view” for each facility (since it seems we cannot pass parameters to views).
Solution 2 : delete in reduce
Thus here is what I tried :
map = function (doc, meta) { if (meta.id.indexOf('product-Walmart-') === 0) { emit(doc.facility, meta.id); // Emits products for all facilities } } reduce = function(key, values, rereduce){ for (i=0; i < values.length; i++) { delete(values[i]); } }Using a startkey=[“NewYork”]
and endkey=[“NewYork”]
, I understand can filter the output of the map function to only perform deletions on the values of the “NewYork” facility.
Unfortunately, it does not seem to work : the associated documents are not deleted…
Any ideas on this matter ?
Thanks in advance,
Antoine