2011-08-05 7 views
4

私はpyMongo 1.11とMongoDB 1.8.2を使用しています。私はかなり複雑なMap/Reduceをやろうとしています。私はMongoの関数を試作し、作業それを得たが、私は、Pythonに転送しようとしたとき、私が取得:'Collection'オブジェクトは呼び出し可能ではありません。そのようなメソッドが存在しないので、 'Collection'オブジェクトの '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 ] } } 

問題の原因が明確ではありません。エラーは、 'Collection'オブジェクトにはmapReduceメソッドがありませんが、http://api.mongodb.org/python/current/examples/map_reduce.htmlの例とコレクションではないものは何ですか?

また、私が以上の20000個のユニークキーを持っているので、なぜ私がgroup()でこれをやっていないのか疑問に思っている場合に備えて。再びページをリンクさ

答えて

0

読み取り、この方法は、その一例でthingsは、あなたがそれで最初の文書を挿入すると、それが作成されますだ、コレクションで、またmap_reduce

と呼ばれています。

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が単一のコレクション、つまりCollectionオブジェクトのインスタンスとして処理され、存在しないオブジェクトに対して__call__メソッドを実行しようとします。したがって、エラー。

pymongoは、このケースを考慮し、存在しない対応するコレクションオブジェクトでmapReduceメソッドを実行しようとしていた可能性を示唆しています。

関連する問題