English 中文(简体)
如何计算MongoDB/Mongoid 中引用模型的发生次数
原标题:How To Count The Occurrence Of A Referenced Model In MongoDB/Mongoid

我在研究一个简单的项目 在那里我有两个样本 参考模型:

class Player
  include Mongoid::Document
  include Mongoid::Timestamps

  has_and_belongs_to_many :games
end

class Game
  include Mongoid::Document
  include Mongoid::Timestamps

  has_and_belongs_to_many :players
end

我需要做的是获得一个与玩家数组玩过的顶级游戏列表。 类似地 :

{
  "diablo_3": {
    "players": 89
  },
  "max_payne_3": {
    "players": 87
  },
  "world_of_warcraft": {
    "players": 65
  },
  "dirt_3": {
    "players": 43
  }
}

谢谢 谢谢

最佳回答

You can use the MongoDB group command to do some server-size processing on a single collection, and it is available as a method on the collection in the Ruby driver, see http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method

Below please find a test based on your models, with the title field added to your Game model. This is a working answer to your question that uses MongoDB s group command.

require  test_helper 

class GameTest < ActiveSupport::TestCase
  def setup
    Player.delete_all
    Game.delete_all
  end

  test "game player count" do
    input = [ [  diablo_3 , 89 ], [  max_payne_3 , 87 ], [  world_of_warcraft , 65 ], [  dirt_3 , 43 ] ]
    input.shuffle.each do | title, count |
      game = Game.create(title: title)
      (0...count).each{ game.players << Player.new }
    end
    game_player_count = Game.collection.group(key: :_id, cond: {}, initial: {count: 0}, reduce:  function(doc, out) { out.title = doc.title; out.count = doc.player_ids.length; } )
    game_player_count.sort!{|a,b| -(a[ count ] <=> b[ count ]) }
    game_player_count = Hash[*game_player_count.map{|r| [r[ title ], {"players" => r[ count ].to_i} ]}.flatten]
    puts JSON.pretty_generate(game_player_count)
  end

end

结果结果结果结果结果

Run options: --name=test_game_player_count

# Running tests:

{
  "diablo_3": {
    "players": 89
  },
  "max_payne_3": {
    "players": 87
  },
  "world_of_warcraft": {
    "players": 65
  },
  "dirt_3": {
    "players": 43
  }
}
.

Finished tests in 0.482286s, 2.0735 tests/s, 0.0000 assertions/s.

1 tests, 0 assertions, 0 failures, 0 errors, 0 skips
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签