Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在mongoid+中找到有2名以上团队参与者的记分卡;铁路3_Ruby On Rails_Ruby On Rails 3_Mongodb_Ruby On Rails 3.1_Mongoid - Fatal编程技术网

Ruby on rails 如何在mongoid+中找到有2名以上团队参与者的记分卡;铁路3

Ruby on rails 如何在mongoid+中找到有2名以上团队参与者的记分卡;铁路3,ruby-on-rails,ruby-on-rails-3,mongodb,ruby-on-rails-3.1,mongoid,Ruby On Rails,Ruby On Rails 3,Mongodb,Ruby On Rails 3.1,Mongoid,嗨,我有以下型号 class Scorecard include Mongoid::Document include Mongoid::Timestamps embeds_many :team_participants end 我想找到所有的记分卡都有两个以上的团队成员。 我试过跟随 Scorecard.where('team_participants.count' => {"$gte" => 3}).count 但这对我不起作用 根据本次谷歌小组讨论,任何帮助都将

嗨,我有以下型号

class Scorecard
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :team_participants
end
我想找到所有的记分卡都有两个以上的团队成员。 我试过跟随

Scorecard.where('team_participants.count' => {"$gte" => 3}).count
但这对我不起作用


根据本次谷歌小组讨论,任何帮助都将不胜感激 似乎无法计算where子句中嵌入文档的长度。但是如果你真的需要这个功能,你可以做一些类似的事情

Scorecard.all.map{|c| c if c.team_participants.length >  3}).compact
但是它会降低性能,因为它会搜索整个文档

或者,您可以检查链接

下面的测试展示了如何通过MongoDB聚合框架实现您想要的功能。 但是,如果选择使用此功能,请仔细测试其性能。 为了使用缩放更高的性能,您应该考虑添加一个显式字段 团队参与者计数,跟踪团队参与者的数组大小, 这个显式字段可以用于普通查询。 如果您尝试其他解决方案,如其他注释中引用的“$where”, 你也应该仔细衡量他们的表现

享受吧

测试/单元/记分卡\u test.rb

require 'test_helper'
require 'pp'

class ScorecardTest < ActiveSupport::TestCase
  def setup
    Scorecard.delete_all
  end

  test "select by array size comparison" do
    docs = [
        {team_participants:[{name:'a'}]},
        {team_participants:[{name:'b'},{name:'c'}]},
        {team_participants:[{name:'d'},{name:'e'},{name:'f'}]},
        {team_participants:[{name:'g'},{name:'h'},{name:'i'},{name:'j'}]}
    ]
    Scorecard.create(docs)
    assert_equal(4, Scorecard.count)
    pp Scorecard.collection.aggregate(
        [{'$unwind' => '$team_participants'},
         {'$group' => {'_id' => '$_id',
                       'team_participants' => {'$push' => '$team_participants'},
                       'team_participants_count' => {'$sum' => 1},
                       'created_at' => {'$first' => '$created_at'},
                       'updated_at' => {'$first' => '$updated_at'}}},
         {'$match' => {'team_participants_count' => {'$gte' => 3}}}
        ])
  end
end
这可能对你有帮助
Run options:

# Running tests:

[1/1] ScorecardTest#test_select_by_array_size_comparison[{"_id"=>"535b1a90a3f576ea5100000a",
  "team_participants"=>
   [{"_id"=>"535b1a90a3f576ea5100000b", "name"=>"g"},
    {"_id"=>"535b1a90a3f576ea5100000c", "name"=>"h"},
    {"_id"=>"535b1a90a3f576ea5100000d", "name"=>"i"},
    {"_id"=>"535b1a90a3f576ea5100000e", "name"=>"j"}],
  "team_participants_count"=>4,
  "created_at"=>2014-04-26 02:31:44 UTC,
  "updated_at"=>2014-04-26 02:31:44 UTC},
 {"_id"=>"535b1a90a3f576ea51000006",
  "team_participants"=>
   [{"_id"=>"535b1a90a3f576ea51000007", "name"=>"d"},
    {"_id"=>"535b1a90a3f576ea51000008", "name"=>"e"},
    {"_id"=>"535b1a90a3f576ea51000009", "name"=>"f"}],
  "team_participants_count"=>3,
  "created_at"=>2014-04-26 02:31:44 UTC,
  "updated_at"=>2014-04-26 02:31:44 UTC}]
Finished tests in 0.031745s, 31.5010 tests/s, 31.5010 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips