私は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()でこれをやっていないのか疑問に思っている場合に備えて。再びページをリンクさ
Ack!それをキャッチするためにありがとう。 –