2012-10-19 62 views
0

我有這樣的map/reduce失敗的Mongoid

class Todo 

    TAG_REGEX = /(?:^|\s)#(\w+)/i 

    include Mongoid::Document 
    include Mongoid::Timestamps 
    include Mongoid::FullTextSearch 

    field :desc, type: String 
    field :done, type: Boolean, default: false 
    field :tags, type: Array 

end 

一類我想要的Map/Reduce這樣

map = %Q{ 
    function() { 
    this.tags.forEach(function(tag){ 
     emit(tag, { count: 1 }); 
    } 
    } 
} 

reduce = %Q{ 
    function(key, values) { 
    var result = { count: 0 }; 
    values.forEach(function(value) { 
     result.count += value.count; 
    }); 
    return result; 
    } 
} 

@map = Todo .map_reduce(map, reduce).out(replace: "tags") 

標籤當我嘗試遍歷結果,我看到這個例外

The operation: #<Moped::Protocol::Command 
    @length=480 
    @request_id=316 
    @response_to=0 
    @op_code=2004 
    @flags=[] 
    @full_collection_name="todozen_development.$cmd" 
    @skip=0 
    @limit=-1 
    @selector={:mapreduce=>"todos", :map=>"\n  function() {\n  this.tags.forEach(function(tag){\n   emit(tag, { count: 1 });\n  }\n  }\n ", :reduce=>"\n  function(key, values) {\n  var result = { count: 0 };\n  values.forEach(function(value) {\n   result.count += value.count;\n  });\n  return result;\n  }\n ", :query=>{}, :sort=>{"created_at"=>-1}, :out=>{:replace=>"tags"}} 
    @fields=nil> 
failed with error "ns doesn't exist" 

我在這裏錯過了什麼?
任何人都需要更多信息?
感謝

+0

我看到Todo類嵌入在User類...所以我想這 '@tags = current_user.todos.where(:done => false).map_reduce(map,reduce).out(replace:「tags」)' 但是這並沒有奏效 我得到了'undefined method'map_reduce 'for#' –

回答

2

Todo是嵌入類...有沒有辦法的Map/Reduce嵌入類...

相關問題