2

mongoid 2 では、これは以前は機能していました。

mr_collection = self.collection.map_reduce(map, reduce, {
  :query => query,
  :finalize => finalize,
  :out => {:replace => 'mr_results'}
})

limit = (options[:limit] || 10)
skip = (options[:skip].to_i || nil)
page = if skip >= limit
  ((skip+limit) / limit)
else
  1
end

sort = if options[:sort_by_vintage]
  [['value.vy', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
elsif options[:sort_by_sDate]
  [['value.sDate', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
else
  [['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
end
paginator = WillPaginate::Collection.new(page, limit, collection_count)
collection = mr_collection.find({},{
    :sort => sort,
    :limit => limit,
    :skip => skip
  }
).to_a

map_reduce 呼び出しを次のように更新しました。

mr_collection = self.where(query).map_reduce(map, reduce).finalize(finalize).out({:replace => 'mr_results'})

もうエラーは発生しませんが、 collection = mr_collection.find.... は、何を試しても常に失敗します。ここにいくつかの試みがあります:

(rdb:1) mr_collection.find.sort(sort)

.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/debug.rb:130:in `eval':wrong number of arguments(1 for 0) を生成します

私はそれを見ることができます (rdb:1) mr_collection.class Mongoid::Contextual::MapReduce

(rdb:1) mr_collection.find.class
Enumerator

試行中: (rdb:1) mr_collection.sort(sort) .rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/debug.rb:130:in `eval': 引数の数が間違っています( 0の場合は1)同じエラー

ご協力いただきありがとうございます

アップデート

次を使用して修正しました:

collection = mr_collection.find(
    :sort => sort,
    :limit => limit,
    :skip => skip
 )

私の問題は現在、collection.to_a を使用していることです。これは、通常のハッシュでうまく機能することがわかっていますが、コレクションの結果は Moped::BSON::Document 型になります。コレクションで Enumerator メソッドを呼び出すと、次のエラーが発生します。

undefined method `call' for #<Hash:

気が狂う。助けてください!!

だから私が試したことには以下が含まれます:

collection = collection.each {|c| c.to_hash}.to_a

collection = collection.collect {|c| c.to_hash}.to_a

ありがとう :)

4

1 に答える 1

3

最後に、Mongoid google グループのおかげで解決しました。詳細はこちら: https://groups.google.com/d/topic/mongoid/T6XhqLtofTE/discussion

ワンライナー修正は次のとおりです。

collection = mr_collection.send(:documents).sort(sort).limit(limit).skip(skip).to_a

次期バージョンの mongoid では、Mongoid::Contextual::MapReduce#documents がプライベート メソッドからパブリック メソッドに変更され、.send(:documents) は不要になります。

于 2012-08-12T21:39:02.120 に答える