2011-08-05 28 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方法的可能性。

相關問題