I have the following collection:
{
"_id" : ObjectId("51f1fcc08188d3117c6da351"),
"cust_id" : "abc123",
"ord_date" : ISODate("2012-10-03T18:30:00Z"),
"status" : "A",
"price" : 25,
"items" : [{
"sku" : "ggg",
"qty" : 7,
"price" : 2.5
}, {
"sku" : "ppp",
"qty" : 5,
"price" : 2.5
}]
}
My map function is:
var map=function(){emit(this._id,this);}
For debugging purpose I overide the emit method as follows:
var emit = function (key,value){
print("emit");
print("key: " + key + "value: " + tojson(value));
reduceFunc2(key, toJson(value));
}
and the reduce function as follows:
var reduceFunc2 = function reduce(key,values){
var val = values;
print("val",val);
var items = [];
val.items.some(function (entry){
print("entry is:::"+entry);
if (entry.qty>5 && entry.sku=='ggg'){
items.push(entry)
}
});
val.items = items;
return val;
}
But when I apply map as:
var myDoc = db.orders.findOne({
_id: ObjectId("51f1fcc08188d3117c6da351")
});
map.apply(myDoc);
I get the following error:
emit key: 51f1fcc08188d3117c6da351 value:
{
"_id":" ObjectId(\"51f1fcc08188d3117c6da351\")",
"cust_id":"abc123",
"ord_date":" ISODate(\"2012-10-03T18:30:00Z\")",
"status":"A",
"price":25,
"items":[
{
"sku":"ggg",
"qty":7,
"price":2.5
},
{
"sku":"ppp",
"qty":5,
"price":2.5
}
]
}
value:: undefined
Tue Jul 30 12:49:22.920 JavaScript execution failed: TypeError: Cannot call method 'some' of undefined
you can find that their is an items field in the value as printed which is of array kind, even then it is throwing error cannot call some on undefined, if someone can tell where i am going wrong.