Mongoid MapReduce error with forEach


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

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

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

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

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

最新更新