Mongoid 3 - 访问map_reduce结果(Mongoid 3 - access map

2019-09-20 17:05发布

在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'})

这不会产生任何错误了,而是集合= mr_collection.find ....总是失败,不管我怎么努力。 这里有一些尝试:

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

其产生.rvm /红宝石/红宝石1.9.3-P194 / LIB /红宝石/ 1.9.1 / debug.rb:130:在`EVAL':错误的参数数目(1 0)

我可以看到(RDB:1)mr_collection.class Mongoid ::语境:: MapReduce的

(rdb:1) mr_collection.find.class
Enumerator

试图:(RDB:1)mr_collection.sort(排序).rvm /红宝石/红宝石1.9.3-P194 / LIB /红宝石/ 1.9.1 / debug.rb:130:在`EVAL':错误数量的参数( 1为0),因此相同的错误

谢谢你的帮助

UPDATE

通过使用固定它:

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

我的问题是现在使用collection.to_a,我知道很适合经常哈希,但集合中的结果类型助力车:: BSON ::文档。 呼吁收集任何枚举的方法,导致此错误:

undefined method `call' for #<Hash:

我要疯了。 请帮忙!!

所以,我试过的东西包括:

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

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

谢谢 :)

Answer 1:

终于找到它了感谢Mongoid谷歌组。 详情点击此处: https://groups.google.com/d/topic/mongoid/T6XhqLtofTE/discussion

在一个班轮解决方法是:

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

在mongoid的即将推出的版本,Mongoid ::语境:: MapReduce的#文件将从私有方法改为一公共和。发送:将不再需要(文档)。



文章来源: Mongoid 3 - access map_reduce results