【问题标题】:pymongo User Assertion: 13606:'out' has to be a string or an objectpymongo 用户断言:13606:'out' 必须是字符串或对象
【发布时间】:2011-11-18 03:59:48
【问题描述】:

我无法弄清楚我在这里做错了什么。我正在使用 pymongo 并具有以下 map/reduce 代码(文档的所有属性都可以直接访问 - 即此处没有相关的嵌入部分:

(在文件 getTableMap.js 中):

function() {
   var tablePoints1 = 0;
   var tablePoints2 = 0;
   if (this.pointsTeam1 == this.pointsTeam2) {
     tablePoints1 = 1;
     tablePoints2 = 1;
   }
   else {
     if (this.pointsTeam1 > this.pointsTeam2) {
       tablePoints1 = 3;
     }
     else {
       tablePoints2 = 3;
     }
   }
   emit(this.idTeam1, [tablePoints1, this.pointsTeam1, this.pointsTeam2]);
   emit(this.idTeam2, [tablePoints2, this.pointsTeam2, this.pointsTeam1]);
}

调用 map_reduce 的 Python 代码如下所示:

def getTableOnMatchday(self):
  m = Code(open('getTableMap.js','r').read())
  r = Code("""function(k,values) { 
    var foo = 'foo';
    return(foo);
  }""")

  result = bl_1.map_reduce(m, r, "myresult")
  for doc in result.find():
    print doc

对于 Python 代码,我直接从文档中改编了简单示例: http://api.mongodb.org/python/current/examples/map_reduce.htmlMap Reduce example from pymongo 2.0.1 documentation

我在运行代码时得到的 Python 回溯是:

>>> api.getTableOnMatchday()
  Traceback (most recent call last):
  pymongo.errors.OperationFailure: command SON([('mapreduce', u'bl1_2011'), 
  ...
  ...
  ...
  ) failed: db assertion failure

这并不能完全告诉我,所以我将 mongod 登录设置为详细,并在日志中发现:

Thu Sep 15 21:04:02 [conn7] User Assertion: 13606:'out' has to be a string
or an object

从实际生成 map_reduce 调用的 Python 代码来看,第三个参数('out',根据 pymongo 2.0.1 文档)是 'myresult',它肯定是一个字符串。

pymongo 在这里抱怨什么? Javascript 在语法上是正确的(我认为)。我知道 reduce 目前什么都不做,但这不应该阻止命令服务器端的编译 - 或者它可能会?

【问题讨论】:

    标签: mapreduce pymongo assertion


    【解决方案1】:

    我想我已经找到了答案,通过更多的试验和错误以及阅读 PHP 驱动程序的文档:

    result = bl_1.map_reduce(m, r, out="foo")
    

    您实际上必须指定 out=string 作为第三个参数。

    文档中的示例在此处误入歧途,正如它所说的那样:

    result = bl_1.map_reduce(m, r, "foo") 
    

    【讨论】:

      【解决方案2】:

      MapReduce 输出选项: v1.8 之前的版本:如果您没有为 out 指定值,则结果将被放入一个临时集合中,该集合的名称将在命令的输出中给出(见下文)。否则,您可以为 out 选项指定集合的​​名称,并将结果放置在那里。

      v1.8+:输出选项已更改。 Map-reduce 不再生成临时集合(因此,keepTemp 已被删除)。

      更多信息可以找到here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多