【问题标题】:Mongoid MapReduce error with forEachforEach 的 Mongoid MapReduce 错误
【发布时间】:2013-07-19 21:41:03
【问题描述】:
The operation: #<Moped::Protocol::Command
  @length=557
  @request_id=4
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="pracker_development.$cmd"
  @skip=0
  @limit=-1
  @selector={:mapreduce=>"pixels", :map=>"\n
  function() {\n
    if(this.tags){\n
      this.tags.forEach(function(tag){\n
        emit(tag, 1)\n
      });\n
    }\n
    }\n          ", :reduce=>"\n
  function(key, values) {\n
    var tag_count = 0 ;\n
    values.forEach(function(value) {\n
      tag_count += value;\n
    });\n
    return tag_count;\n
    }\n          ", :query=>{}, :out=>{:inline=>true}}
  @fields=nil>
failed with error 9014: "exception: map invoke failed: JS Error: TypeError: this.tags.forEach is not a function nofile_b:2"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.

使用 Pixel.all_tags 会发生此错误

我过去有过这种情况,但我能想出的唯一解决方案是删除集合并重新开始......

github 问题: https://github.com/chebyte/mongoid-simple-tags/issues/21

【问题讨论】:

    标签: ruby-on-rails mongodb mapreduce mongoid


    【解决方案1】:

    我认为可能不会在所有文档上设置标签。在这种情况下,它可能是未定义的,因此没有 forEach 方法。

    解决此问题的一种方法是将查询传递给 map/reduce 命令,指定 tags 字段必须存在:

    { query : { tags : { '$exists': true } } }

    如果它也可能不是数组,您还可以通过检查它的类型来检查它是否是一个数组。

    【讨论】:

    • 感谢您的提示,我会尝试看看是否可以解决问题。如果是这样,我将在 mongoid-simple-tags repo 上提交一个拉取请求。
    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多