Array Return from Reduce

165 views
Skip to first unread message

J Singh

unread,
Aug 1, 2011, 8:28:22 AM8/1/11
to mongod...@googlegroups.com
The Map/Reduce documentation says: "Currently, the return value from a reduce function cannot be an array (it's typically an object or a number)."

Does anyone have a suggestion on how we might be able to implement it with Mongo 1.8x? This is what we are trying to accomplish, although it doesn't work because of the above restriction.
map = function() { emit(this.FnName, [this.FnAddress]); }
red = function(k,v) { var addrs=[]; for (var i in v) var addr = v[i][0]; if(addrs.indexOf(addr) < 0)addrs.push(addr); return addrs;}
res = db.MyColl.mapReduce(map, red, {query: { "Timestamp" : { $gte: 1, $lte: 15 } }, out: "slice_fn_addrs"})

Thanks.

Nat

unread,
Aug 1, 2011, 8:40:27 AM8/1/11
to mongod...@googlegroups.com
you just wrap it inside an object something like:

map = function() { emit(this.FnName, { "arr" : this.FnAddress]); }
red = function(k,v) { var addrs=[]; for (var i in v.arr) var addr = v.arr[i][0]; if(addrs.indexOf(addr) < 0)addrs.push(addr); return { "arr" : addrs };}



Scott Hernandez

unread,
Aug 1, 2011, 8:43:05 AM8/1/11
to mongod...@googlegroups.com
You can put it in an object/doc as the documentation suggests:

map:
...
emit(this.FnName, {addresses: [this.FnAddress]});

reduce:
...
return {addresses: addrs}

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/8YRPQos2wfAJ.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>

Reply all
Reply to author
Forward
0 new messages