Collection object cannot be called. If you want to call the mapReduce method on the Collection object, it does not work because such a method does not exist

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 .

+4
source share
3 answers

It is not called mapReduce , but map_reduce . Try:

 results = db.user_actions.map_reduce(map, reduce, "user_entities_interactions") 
+5
source

Problem

As all answers mentioned, the problem is that the MapReduce method in pymongo is actually written with underscore, i.e. map_reduce to fit into the most used Python code style.

Startup error

 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. 

A mistake can seem very confusing and lead you in the wrong direction. The point here is that MongoDB uses the names of internal / system collections that use a dot, for example. system.namespaces , system.indexes , system.profile , etc. Although MongoDB does not allow you to use the dot-ed name to create a new collection, you can query existing system collections anyway. Therefore, while you run your user_actions.mapReduce code, it actually treats user_actions.mapReduce as a single set, that is, an instance of the Collection object, and then tries to execute the __call__ method on this object that does not exist. So the error.

The good part is that pymongo considers this case and hints at the possibility that you tried to execute the mapReduce method on the corresponding Collection object that does not exist.

+2
source

read this linked page again, the method is called map_reduce

also, in this example things is a collection, it is created when you insert the first document into it.

0
source

All Articles