Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method" rel="nofollow">http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method</a></p> <p>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.</p> <pre><code>require 'test_helper' class GameTest &lt; 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 &lt;&lt; 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'] &lt;=&gt; b['count']) } game_player_count = Hash[*game_player_count.map{|r| [r['title'], {"players" =&gt; r['count'].to_i} ]}.flatten] puts JSON.pretty_generate(game_player_count) end end </code></pre> <p>result</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload