Page 1 of 1

code a script2.js file that does a map reduce of the customers collections and produces a report that shows zip codes th

Posted: Mon Jun 06, 2022 12:36 pm
by answerhappygod
code a script2.js file that does a map reduce of the customers
collections and produces a report that shows zip codes that start
with ‘9’ and the count of customers for each zip code.

here is a short sample of the customers collection
db.customers.insertMany( [
{
"customerId": 1,
"customer_name": "US Postal Service",
"address": {
"street": "Attn:
Supt. Window Services; PO Box 7005",
"city": "WI",
"state": "Madison",
"zip": "53707"
},
"contact": {
"last_name":
"Alberto",
"first_name":
"Francesco"
}
},
{
"customerId": 2,
"customer_name": "National Information Data
Ctr",
"address": {
"street": "PO Box
96621",
"city": "DC",
"state":
"Washington",
"zip": "20120"
},
"contact": {
"last_name":
"Irvin",
"first_name": "Ania"
}
},
{
"customerId": 3,
"customer_name": "Register of Copyrights",
"address": {
"street": "Library Of
Congress",
"city": "DC",
"state":
"Washington",
"zip": "20559"
},
"contact": {
"last_name":
"Liana",
"first_name":
"Lukas"
}
},
{
"customerId": 4,
"customer_name": "Jobtrak",
"address": {
"street": "1990 Westwood
Blvd Ste 260",
"city": "CA",
"state": "Los
Angeles",
"zip": "90025"
},
"contact": {
"last_name":
"Quinn",
"first_name":
"Kenzie"
}
},
{
"customerId": 5,
"customer_name": "Newbrige Book Clubs",
"address": {
"street": "3000 Cindel
Drive",
"city": "NJ",
"state":
"Washington",
"zip": "07882"
},
"contact": {
"last_name":
"Marks",
"first_name":
"Michelle"
}
},
{
"customerId": 6,
"customer_name": "California Chamber Of
Commerce",
"address": {
"street": "3255 Ramos
Cir",
"city": "CA",
"state":
"Sacramento",
"zip": "95827"
},
"contact": {
"last_name":
"Mauro",
"first_name":
"Anton"
}
}
]);
--------------------------------------------------------------------------------------------------------------------------------------
This is what I have so far, but I think I need to set the key to
the zipcode but because it is nested inside of
customers.address.zip I am unsure how.
mapz = function() {
address = this.address;
zip = address.zip;
emit(this.customerId, {zipcode:zip})
}
reducez = function(key, values) {
for (x of values) {
zip = x.zipcode;

}
if (zip.startsWith('9') )
return {zipcode:zip};
}
emit = function(key, value) {
print("key:", key, "value:");
printjsononeline(value);
}
print("Map test:");
q = db.customers.find();
while (q.hasNext()) {
doc = q.next();
printjsononeline(doc);
mapz.apply(doc);
}
db.customers.mapReduce(mapz, reducez, {out: "example1"});
q = db.example1.find().sort( { _id:1 } );
print("Output from map reduce.");
while ( q.hasNext() ){
printjsononeline(q.next());
}

If you are feeling spicy this is the next question that myself and
others also need help on.
code a script3.js file that does a map reduce that answers this
question? What is the average
quantity for orders? If an order contains
items: [{ itemNo: 1, qty: 4 }, { itemNo: 2, qty: 1} ]
the total quantity for this order is 5.
Your script calculates the average quantity and displays a single
number.