How to emit more than two parameters using MongoDB MapReduce

1,431 views
Skip to first unread message

munavar fairooz

unread,
Feb 14, 2015, 6:09:51 AM2/14/15
to mongod...@googlegroups.com
I have a collection called 'orders' having lot of documents of the form

{
     _id
: 123
     cust_id
: "abc123",
     ord_date
: new Date("Oct 04, 2012"),
     status
: 'A',
     price
: 25,
   
}




Now i want to
emit(cust_id, Price,_id) through map function .
Here
 Cust_id =>Key
 
Price =>value
 _Id
=> Provenance Parameter



    In the Reduce function i will find the total Price of each cust_id and also , i want to store the  _id values of the documents that contributed to the total price of each cust_id into a separate  Provenance collection.
Map Reduce function support only 2 arguments. Is there any way to solve the problem??

Thank you....











Asya Kamsky

unread,
Feb 14, 2015, 10:59:59 PM2/14/15
to mongodb-user
The second argument can be a document, so emit something like

{price: this.price, _id: [ this._id ] }

You will need to adjust your reduce to return the same type, so I assume you want eventually a list of _id's, you need to emit an  array.

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/7663c675-fd61-43a7-9a34-49559af49913%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
{ "name" : "Asya Kamsky",
  "place" : [ "New York", "Palo Alto", "Everywhere else" ],
  "email" : "as...@mongodb.com",
  "blog" : "http://www.askasya.com/",
  "twitter": "@asya999" }
Message has been deleted

Asya Kamsky

unread,
Feb 17, 2015, 6:37:35 PM2/17/15
to mongodb-user
The problem is your reduce function.

When you have an array values of this form:

[  { price: 15, _id: [ ObjectId("...") ] }, { price: 27, _id: [ ObjectId("...") ] } ]

You cannot reduce that to a single { price: xxx, _id: [ ..., ..., ...] } the way you are doing, you will actually need to iterate over values array accumulating prices and concatenating arrays elements.

Asya
 

On Mon, Feb 16, 2015 at 12:58 AM, munavar fairooz <m4mu...@gmail.com> wrote:
Sir,
what i want in the result collection is
{_id:cust_id,
value
: {total: sum(price), id:[array of id's corresponding documents]}
}


As you said, i have written my Map function as
var mapFunction1 = function() {
                       emit
(this.cust_id, {price:this.price, _id: [ this._id ]})
                   
};
var reduceFunction1 = function(keyCustId, value) {
                         
                         
return {total:Array.sum(value['price']), id:Array([value['_id']])};
                     
};
db
.order.mapReduce( mapFunction1,reduceFunction1,{ out: "map_reduce_example" });


But its showing an error like
 Error: map reduce failed:{
    "errmsg" : "exception: TypeError: Cannot read property 'length' of undefined\n    at Function.Array.sum (src/mongo/shell/types.js:192:12)\n    at _funcs2 (_funcs2:3:47) at src/mongo/shell/types.js:192",
    "code" : 16722,
    "ok" : 0
}
    at Error (<anonymous>)
    at DBCollection.mapReduce (src/mongo/shell/collection.js:1227:15)
    at (shell):1:10 at src/mongo/shell/collection.js:1227


Whats wrong with the program??
thankyou....




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages