在 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': 参数数量错误(1 代表 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': 参数数量错误( 1 为 0) 所以同样的错误
谢谢你的帮助
更新
通过使用修复它:
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
谢谢 :)