I have a map function that filters out documents based on a date range and it works as expected. If I run a similar N1QL query I get the same results as the view map. However, when I reduce via CB’s internal _stats function, I get a HIGHER record count than without the reduce.
Map Function (w/o reduce):
function (doc, meta) {
// These are Magento statuses
var statuses = ['complete', 'pending', 'processing'];
if (doc.type == 'mage-sales-order') {
var soDate = new Date(doc.salesorder.MageSalesOrder.created_at);
var rolling = new Date();
rolling.setDate(rolling.getDate() - 6);
rolling.setHours(0, 0, 0, 0);
if (statuses.indexOf(doc.salesorder.MageSalesOrder.status) > -1 && soDate >= rolling) {
var total = Number(doc.salesorder.MageSalesOrder.grand_total);
if (total != "NaN" && total > 0) {
emit(meta.id, total);
}
}
}
}
Notice the record count is now 9 and the “sum” value is incorrect, too (summing the 7 results from the pure map function yields a lower amount, as expected).