2011-08-05 134 views
4

我使用pyMongo 1.11和MongoDB 1.8.2。我正在尝试做一个相当复杂的Map/Reduce。我在原型蒙戈的功能,得到了它的工作,但是当我试图将其传送到Python,我得到:'集合'对象不可调用。如果你打算叫“MapReduce的”方法“集合”对象是失败的,因为没有这样的方法存在

--------------------------------------------------------------------------- 
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. 

我的收藏是这样的:

{ "_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 } 

我运行的代码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") 

什么结果应该样子是:

{ "_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 ] } } 

我不上的问题是什么。错误说,“收藏”对象没有MapReduce的方法,但是这显然不是作为例子真正在http://api.mongodb.org/python/current/examples/map_reduce.html作品,什么是“东西”,如果不是一个集合?

此外,如果你想知道为什么我不用group()这样做,这是因为我有超过20000个唯一键

回答

0

读一遍链接的页面,该方法被称为map_reduce

而且,在例如things的集合,它得到,当你在它插入第一个文档创建。

5

这不叫mapReduce,但map_reduce。尝试:

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

Ack!感谢您的支持。 –

2

的问题

正如在所有的答案中提到的问题是,在pymongo MapReduce的方法实际上是写有下划线,即map_reduce,以适合最常使用Python代码样式。

混乱的错误

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. 

错误可能看起来很混乱,带你在错误的方向。这里的要点是,MongoDB使用内部/系统集合名称,例如使用,例如, system.namespacessystem.indexessystem.profile等。虽然MongoDB的不让你使用 -ed名称创建一个新的集合,反正你可以查询现有的系统集合。所以,当你运行你的user_actions.mapReduce代码,它实际上把user_actions.mapReduce作为一个单一的集合,即集合对象的实例,然后尝试给该对象,不存在上执行__call__方法。因此错误。

好的部分是pymongo认为这种情况,并暗示您试图对不存在的相应Collection对象执行mapReduce方法的可能性。