Ruby 计算二级嵌套模型(通常使用mongoid或MongoDB)的有效方法是什么?

Ruby 计算二级嵌套模型(通常使用mongoid或MongoDB)的有效方法是什么?,ruby,mongodb,ruby-on-rails-3,mongoid,database,Ruby,Mongodb,Ruby On Rails 3,Mongoid,Database,我有以下数据模型。其中一个项目嵌入了许多ComponentDescriptor,一个ComponentDescriptor嵌入了许多统计信息 class Project include Mongoid::Document embeds_many :component_descriptors field :status, :type => Integer backgrounded :publish end class ComponentDescriptor inc

我有以下数据模型。其中一个项目嵌入了许多ComponentDescriptor,一个ComponentDescriptor嵌入了许多统计信息

class Project
  include Mongoid::Document
  embeds_many :component_descriptors

  field :status, :type => Integer

  backgrounded :publish 
end

class ComponentDescriptor
  include Mongoid::Document
  include Mongoid::Acts::Tree

  embeds_many :statistics
  embedded_in :project, :inverse_of => :component_descriptors

end


class Statistic
  include Mongoid::Document
  field :statistics_type, :type => String
  field :data, :type => String
  field :playhead_time, :type => String
  field :remote_ip, :type => String
  field :user_agent, :type => String

  embedded_in :component_descriptor, :inverse_of => :statistics
end
问题是什么是计算项目中统计对象总数的最佳方法

我能想到的一种方法是循环遍历每个ComponentDescriptor,计算统计对象的数量,然后将它们相加。但我认为这不是一个有效的方法


提前感谢。

如果您可以在项目级别存储count字段,这将是您获得它的最佳和最快的方式