首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“‘Collection”对象不可调用。如果您打算在'Collection‘对象上调用'mapReduce’方法,它将失败,因为不存在这样的方法。

问“‘Collection”对象不可调用。如果您打算在'Collection‘对象上调用'mapReduce’方法,它将失败,因为不存在这样的方法。
EN

Stack Overflow用户
提问于 2011-08-05 21:45:31
回答 4查看 16K关注 0票数 5

我使用的是pyMongo 1.11和MongoDB 1.8.2。我正在尝试做一个相当复杂的Map/Reduce。我在Mongo中对函数进行了原型化,并使其正常工作,但当我尝试将其传输到Python时,我得到了:

代码语言:javascript
复制
---------------------------------------------------------------------------
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.

我的收藏品是这样的:

代码语言:javascript
复制
{ "_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中运行的代码是:

代码语言:javascript
复制
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")

结果应该是:

代码语言:javascript
复制
{ "_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方法,但是在reduce.html的示例中,这显然不是正确的,如果不是集合,什么是'things’?

另外,如果您想知道我为什么不使用group()来执行这个操作,这是因为我有20000多个唯一的键。

EN

回答 4

Stack Overflow用户

发布于 2011-08-05 21:51:06

它不是mapReduce,而是map_reduce。尝试:

代码语言:javascript
复制
results = db.user_actions.map_reduce(map, reduce, "user_entities_interactions")
票数 7
EN

Stack Overflow用户

发布于 2015-05-05 22:01:43

问题

正如所有答案中提到的,问题是pymongo中的pymongo方法实际上是用下划线(即reduce )编写的,以适应最常用的Python代码风格。

混淆错误

代码语言:javascript
复制
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.namespaces、system.indexes、system.profile等。尽管MongoDB不允许您使用点式名称创建新集合,但是您可以查询现有的系统集合。因此,在运行user_actions.mapReduce代码时,它实际上将user_actions.mapReduce视为单个集合,即collection对象的实例,然后尝试在不存在的对象上执行__call__方法。因此,错误。

更好的是,pymongo考虑了这种情况,并暗示您可能试图在不存在的相应Collection对象上执行mapReduce方法。

票数 2
EN

Stack Overflow用户

发布于 2011-08-05 21:50:01

再次读取该链接页,该方法称为map_reduce

另外,在这个示例中,things是一个集合,当您在其中插入第一个文档时,它将被创建。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6962952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档