新手问题 如何在 rails 中读取 mongodb 的数据

jacobsen · 发布于 2013年08月02日 · 最后由 mvj3 回复于 2013年08月02日 · 1557 次阅读
96

要实现的功能大致是:统计几个collection中的数据,整合然后列出 问题是:如何在rails中读取这些collection中的数据

共收到 2 条回复
29

如果只是简单的count,而且这几个collection要统计的数据结构都是一致的,推荐使用我正在开发的面向ActiveRecord和Mongoid统计分析的gem包statlysis,项目地址在: https://github.com/eoecn/statlysis

使用方法如下:

批量初始化model和表结构

{'07' => 1..31, '08' => 1..12}.map do |month, day_range|
  day_range.map do |day|
    # define model dynamically, e.g. MultipleLog20130729
    date_str = "2013#{month}#{day.to_s.rjust(2, '0')}"
    collection_class_name = "MultipleLog#{date_str}"
    collection_name = collection_class_name.sub("MultipleLog", "multiple_log_")

    eval("
      class #{collection_class_name}
        include Mongoid::Document
        self.default_collection_name = #{collection_name.to_json}
        field :t, :type => DateTime
        field :url, :type => String
        index({t: -1}, {:background => true})
      end
    ")
  end
end

简单的配置

Statlysis.setup do
  set_database :statlysis

  daily Mongoid[/eoe_logs_[0-9]+$/].where(:ui => {"$ne" => 0}), :time_column => :t
end

访问和统计

[23] pry(#<Statlysis::Configuration>)> Statlysis.daily['multi'].first
=> #<Statlysis::Timely:0x7fb9cc60a490 @is_activerecord=#<FalseClass:0x0>, @is_mongoid=#<TrueClass:0x4>, @is_orm=#<TrueClass:0x4>, @time_column=:t, @time_unit=:day, @time_zone=#<NilClass:0x8>, @multiple_dataset=#<Statlysis::MongoidDataset:0x7fb9cc67e840>, @stat_table_name="timely_multiplelog_d", @stat_model=#<Class:0x7fb9cc6116c8>, @output=#<Array:0x7fb9cc798118>>
[24] pry(#<Statlysis::Configuration>)> Statlysis.daily['multi'].first.output
=> [{:t=>Mon, 01 Jul 2013 00:00:00 +0000, :timely_c=>1, :totally_c=>1},
 {:t=>Tue, 02 Jul 2013 00:00:00 +0000, :timely_c=>2, :totally_c=>3},
 {:t=>Wed, 03 Jul 2013 00:00:00 +0000, :timely_c=>3, :totally_c=>6},
 {:t=>Thu, 04 Jul 2013 00:00:00 +0000, :timely_c=>4, :totally_c=>10},
 {:t=>Fri, 05 Jul 2013 00:00:00 +0000, :timely_c=>5, :totally_c=>15},
 {:t=>Sat, 06 Jul 2013 00:00:00 +0000, :timely_c=>6, :totally_c=>21},
 {:t=>Sun, 07 Jul 2013 00:00:00 +0000, :timely_c=>7, :totally_c=>28},
 {:t=>Mon, 08 Jul 2013 00:00:00 +0000, :timely_c=>8, :totally_c=>36},
 {:t=>Tue, 09 Jul 2013 00:00:00 +0000, :timely_c=>9, :totally_c=>45},
 {:t=>Wed, 10 Jul 2013 00:00:00 +0000, :timely_c=>10, :totally_c=>55},
 {:t=>Thu, 11 Jul 2013 00:00:00 +0000, :timely_c=>11, :totally_c=>66},
 {:t=>Fri, 12 Jul 2013 00:00:00 +0000, :timely_c=>12, :totally_c=>78},
 {:t=>Sat, 13 Jul 2013 00:00:00 +0000, :timely_c=>13, :totally_c=>91},
 {:t=>Sun, 14 Jul 2013 00:00:00 +0000, :timely_c=>14, :totally_c=>105},
 {:t=>Mon, 15 Jul 2013 00:00:00 +0000, :timely_c=>15, :totally_c=>120},
 {:t=>Tue, 16 Jul 2013 00:00:00 +0000, :timely_c=>16, :totally_c=>136},
 {:t=>Wed, 17 Jul 2013 00:00:00 +0000, :timely_c=>17, :totally_c=>153},
 {:t=>Thu, 18 Jul 2013 00:00:00 +0000, :timely_c=>18, :totally_c=>171},
 {:t=>Fri, 19 Jul 2013 00:00:00 +0000, :timely_c=>19, :totally_c=>190},
 {:t=>Sat, 20 Jul 2013 00:00:00 +0000, :timely_c=>20, :totally_c=>210},
 {:t=>Sun, 21 Jul 2013 00:00:00 +0000, :timely_c=>21, :totally_c=>231},
 {:t=>Mon, 22 Jul 2013 00:00:00 +0000, :timely_c=>22, :totally_c=>253},
 {:t=>Tue, 23 Jul 2013 00:00:00 +0000, :timely_c=>23, :totally_c=>276},
 {:t=>Wed, 24 Jul 2013 00:00:00 +0000, :timely_c=>24, :totally_c=>300},
 {:t=>Thu, 25 Jul 2013 00:00:00 +0000, :timely_c=>25, :totally_c=>325},
 {:t=>Fri, 26 Jul 2013 00:00:00 +0000, :timely_c=>26, :totally_c=>351},
 {:t=>Sat, 27 Jul 2013 00:00:00 +0000, :timely_c=>27, :totally_c=>378},
 {:t=>Sun, 28 Jul 2013 00:00:00 +0000, :timely_c=>28, :totally_c=>406},
 {:t=>Mon, 29 Jul 2013 00:00:00 +0000, :timely_c=>29, :totally_c=>435},
 {:t=>Tue, 30 Jul 2013 00:00:00 +0000, :timely_c=>30, :totally_c=>465},
 {:t=>Wed, 31 Jul 2013 00:00:00 +0800, :timely_c=>27, :totally_c=>483}]
[25] pry(#<Statlysis::Configuration>)> 
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册