gpt4 book ai didi

python - “集合”对象不可调用。如果你打算在 'mapReduce' 对象上调用 'Collection' 方法,它会失败,因为不存在这样的方法

转载 作者:可可西里 更新时间:2023-11-01 09:11:05 27 4
gpt4 key购买 nike

我正在使用 pyMongo 1.11 和 MongoDB 1.8.2。我正在尝试做一个相当复杂的 Map/Reduce。我在 Mongo 中制作了这些函数的原型(prototype)并让它运行起来,但是当我尝试将它转移到 Python 时,我得到:

---------------------------------------------------------------------------
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 中的示例不符。有效,如果不是集合,“事物”又是什么?

此外,如果您想知道为什么我不使用 group() 执行此操作,那是因为我有超过 20000 个唯一键

最佳答案

它不叫 mapReduce,而是 map_reduce .尝试:

results = db.user_actions.map_reduce(map, reduce, "user_entities_interactions")

关于python - “集合”对象不可调用。如果你打算在 'mapReduce' 对象上调用 'Collection' 方法,它会失败,因为不存在这样的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6962952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com