Mongoid 3-访问map_reduce结果 [英] Mongoid 3 - access map_reduce results
问题描述
在蒙古族2中,这曾经起作用:
In mongoid 2, this used to work:
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调用更新为:
I have updated the map_reduce call to be:
mr_collection = self.where(query).map_reduce(map, reduce).finalize(finalize).out({:replace => 'mr_results'})
这不再产生任何错误,但是不管我尝试什么,collection = mr_collection.find ....总是失败.这里有一些尝试:
Which does not produce any errors any more, but the collection = mr_collection.find.... always fails no matter what I try. Here are a few attempts:
(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)
which produces .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
I can see that (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:在'eval'中:错误的参数数量(1表示0) 同样的错误
Trying: (rdb:1) mr_collection.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) so same error
感谢您的帮助
使用以下方法修复了该问题:
Fixed it by using:
collection = mr_collection.find(
:sort => sort,
:limit => limit,
:skip => skip
)
我的问题是现在使用collection.to_a,我知道它对于常规散列效果很好,但是集合中的结果类型为Moped :: BSON :: Document.在集合上调用任何Enumerator方法都会导致此错误:
My problem is now using collection.to_a, which I know works well for regular hashes, but the results in the collection are of type Moped::BSON::Document. Calling any Enumerator method on collection, results in this error:
undefined method `call' for #<Hash:
我要疯了.请帮忙!
所以我尝试过的事情包括:
So of the things I tried include:
collection = collection.each {|c| c.to_hash}.to_a
和
collection = collection.collect {|c| c.to_hash}.to_a
谢谢:)
推荐答案
最后归功于Mongoid google组.此处的详细信息: https://groups.google.com/d/topic/mongoid/T6XhqLtofTE/讨论
Finally figured it out thanks to the Mongoid google group. Details here: 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).
In an upcoming version of mongoid, Mongoid::Contextual::MapReduce#documents will be changed from a private method to a public one, and the .send(:documents) won't be needed anymore.
这篇关于Mongoid 3-访问map_reduce结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!