Hi, i'm a new CouchDB user and i'm working on this documents:
- Customer: { customerID, customerName, address, branch,
branchID}
- Product: { productID, productName, category_1ID,
category_1Name, ...
category_xID, category_xName, priceStandard}
- Orders: { orderID, orderRowNumber, customerID,
productID, price, quantity, date}
Now, i must get some statistics based on customerID, and i wrote this
map/reduce functions:
function(doc) {
if (doc.type == "customer") {
emit(doc._id, doc);
} else if (doc.type == "order") {
emit(doc.customer_id, doc);
}
}
function (keys, values, re) {
var numProducts = 0,
numOrders = 0,
totalValue = 0,
customerName, branch, branchID;
for (var item in values) {
if (values[item].type === "customer") {
branchID = values[item].branch_id;
branch = values[item].branch;
customerName = values[item].customer_name;
}
if (values[item].type === "order") {
totalValue += (values[item].price *
values[item].quantity);
numOrders += 1;
if (values[item].product_id) {
numProducts += 1;
}
}
}
var results = {customerID: keys[0][0], customerName: customerName,
branchID: branchID, branch: branch, numProducts: numProducts,
numOrders: numOrders, totalValue: totalValue};
return results;
}
I must get the category count for each productID and return with the
other results of the view, so i should do a "Join" based on productID
from Order and Product. I saw some example online, but i'm confused
because i can't use my function with customerID and i can't retrieve
the results.
Could someone give me some suggestions about that?
Thanks for your time.
Best regards.
--
Andrea Di Mario
LinkedIn: http://www.linkedin.com/in/andreadimario