我使用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個唯一鍵。
Ack!感謝您的支持。 –