【问题标题】:How do I get unique elements in this array?如何在此数组中获取唯一元素?
【发布时间】:2010-11-23 02:21:25
【问题描述】:

使用 Mongoid。不幸的是,Mongoid 不允许选择唯一/不同的! 得到了这些结果。如您所见,有 7 个结果。如果你仔细看(user_id),只有 2 个用户。

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6c3072357e00fa000116, created_at: 2010-11-22 13:12:16 UTC, updated_at: 2010-11-22 13:12:16 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6bdd72357e00fa00010d, created_at: 2010-11-22 13:10:53 UTC, updated_at: 2010-11-22 13:10:53 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea46df72357e00fa0000a4, created_at: 2010-11-22 10:33:03 UTC, updated_at: 2010-11-22 10:33:03 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea40c572357e00fa00006f, created_at: 2010-11-22 10:07:01 UTC, updated_at: 2010-11-22 10:07:01 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea344a72357e00fa00003f, created_at: 2010-11-22 09:13:46 UTC, updated_at: 2010-11-22 09:13:46 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea306c72357e00fa000031')>
] 

我正在查看this,并认为我可以做一些类似的事情,这样我的数组现在看起来像这样:

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>
]

我不关心提取了哪些结果组合。只要我在结果集中有唯一的 user_id。有谁知道这是怎么实现的?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您可以只使用uniq 方法。假设你的数组是ary,调用:

    ary.uniq{|x| x.user_id}
    

    这将返回一个具有唯一user_ids 的集合。

    【讨论】:

    • 这是对 OP 问题的快速解决方案,但从长远来看,它鼓励将所有数据吞入其中,然后对其进行迭代以删除重复项。这将消耗不必要的资源。一个更好的解决方案是让 OP 使用更智能的查询来预先消除欺骗,或者使用从本质上消除欺骗的集合类型。
    • @Greg:是的,我同意,但请确保您也没有进行过早的优化......
    • 这并不为时过早,这是 30 多年处理数据库的经验。
    • @Peter 它返回一个数组而不是一个集合。见ruby-doc.org/core-2.2.0/Array.html
    【解决方案2】:

    这应该适合你:

    考虑 Table1 有一个名为 activity 的列,它可能在多个记录中具有相同的值。这就是您将仅提取 Table1 中 activity 字段的唯一条目的方法。

    #An array of multiple data entries
    @table1 = Table1.find(:all) 
    
    #extracts **activity** for each entry in the array @table1, and returns only the ones which are unique 
    
    @unique_activities = @table1.map{|t| t.activity}.uniq 
    

    【讨论】:

      【解决方案3】:

      对于将来遇到此问题的人,您现在可以使用 Origin 的 Mongoid::Criteria#distinct 方法从数据库中仅选择不同的值:

      # Requires a Mongoid::Criteria
      Attendees.all.distinct(:user_id)
      

      http://mongoid.org/en/mongoid/docs/querying.html (v3.1.0)

      【讨论】:

        【解决方案4】:

        你看过这个页面吗?

        http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

        这可能会为您节省一些时间?

        例如 db.addresses.distinct("邮编");

        【讨论】:

          【解决方案5】:

          考虑使用HashSet,而不是使用数组。

          集合的行为类似于数组,只是它们只包含唯一值,并且在幕后是建立在哈希之上的。与数组不同,集合不保留项目放入其中的顺序。哈希也不保留顺序,但可以通过键访问,因此您不必遍历哈希来查找特定项目。

          我喜欢使用哈希。在您的应用程序中,user_id 可以是键,值是整个对象。这将自动从哈希中删除所有重复项。

          或者,只从数据库中提取唯一值,就像 John Ballinger 建议的那样。

          【讨论】:

          • 注:从 Ruby 1.9 开始,哈希的顺序保持不变,所以我假设是集合。如果我在后者上错了,很高兴得到纠正。
          【解决方案6】:

          Errrr,视图有点乱。但我认为我已经让它与小组合作 (http://mongoid.org/docs/querying/)

          控制器

          @event_attendees = Activity.only(:user_id).where(:action => 'Attend').order_by(:created_at.desc).group
          

          查看

          <% @event_attendees.each do |event_attendee| %>    
            <%= event_attendee['group'].first.user.first_name %>
          <% end %>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-08
            • 2018-06-24
            相关资源
            最近更新 更多