I am using pyMongo 1.11 and MongoDB 1.8.2. I am trying to make a rather complicated map / reduce. I prototyped functions in Mongo and got it working, but when I tried passing it to Python, I got:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Developer/R-and-D/<ipython-input-71-3c3a43221538> in <module>() ----> 1 results = db.user_actions.mapReduce(map, reduce, "user_entities_interactions") /Library/Python/2.7/site-packages/pymongo/collection.pyc in __call__(self, *args, **kwargs) 1099 "call the '%s' method on a 'Collection' object it is " 1100 "failing because no such method exists." % -> 1101 self.__name.split(".")[-1]) TypeError: 'Collection' object is not callable. If you meant to call the 'mapReduce' method on a 'Collection' object it is failing because no such method exists.
My collection is as follows:
{ "_id" : ObjectId("..."), "entity_id" : 1556, "user_id" : 466112 } { "_id" : ObjectId("..."), "entity_id" : 1366, "user_id" : 10057 } { "_id" : ObjectId("..."), "entity_id" : 234, "user_id" : 43650 } { "_id" : ObjectId("..."), "entity_id" : 6, "user_id" : 34430 } { "_id" : ObjectId("..."), "entity_id" : 461, "user_id" : 3416 } { "_id" : ObjectId("..."), "entity_id" : 994, "user_id" : 10057 } { "_id" : ObjectId("..."), "entity_id" : 296, "user_id" : 466112 }
The code I run in Python:
map = Code("""function () { emit(this.user_id, { user_id : this.user_id, entity_id : this.entity_id}); }""") reduce = Code("""function (key, values) { var entities = { user_id : values[0].user_id, entity_id : [ ] }; for (var i = 0; i < values.length; i++) { entities.entity_id[i] = values[i].entity_id; } return entities; }""") results = db.user_actions.mapReduce(map, reduce, "user_entities_interactions")
What the result looks like:
{ "_id" : 3416, "value" : { "user_id" : 3416, "entity_id" : 461 } } { "_id" : 10057, "value" : { "user_id" : 10057, "entity_id" : [ 1366, 994 ] } } { "_id" : 34430, "value" : { "user_id" : 34430, "entity_id" : 6 } } { "_id" : 43650, "value" : { "user_id" : 43650, "entity_id" : 234 } } { "_id" : 466112, "value" : { "user_id" : 466112, "entity_id" : [ 1556, 296 ] } }
I do not understand what the problem is. The error says that the "Collection" object does not have a mapReduce method, but this is clearly not the case as shown in the example http://api.mongodb.org/python/current/examples/map_reduce.html , and what are the 'things if not a collection?
Also, if you are wondering why I am not doing this with group (), because I have over 20,000 unique keys .