如何在这个数组中获得唯一的元素?

使用 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')>
]

我看着 这个,想着我可以做一些类似的事情,让我的数组看起来像这样:

[
#<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。有人知道这是怎么做到的吗?

114243 次浏览

不要使用 Array,可以考虑使用 大麻预备

集合的行为类似于 Array,只不过它们只包含唯一的值,而且它们是在 Hash 基础上构建的。与 Array 不同,Set 不保留项放入它们的顺序。散列也不保留顺序,但可以通过键访问,因此不必遍历散列来查找特定项。

我喜欢使用 Hash。在应用程序中,user _ id 可以是键,值可以是整个对象。它将自动从散列中删除任何重复项。

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

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

ary.uniq{|x| x.user_id}

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

你看过这一页吗?

Http://www.mongodb.org/display/docs/aggregation#aggregation-distinct

这样可以节省你的时间吗?

例句 清晰的(“邮政编码”) ;

呃,看起来有点乱,但是我想我已经让它和小组( 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 %>

这应该对你有用:

考虑到表1有一个名为 活动的列,它可能在多个记录中具有相同的值。这就是在表1中只提取 活动字段的唯一条目的方法。

#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

对于那些在将来遇到这个问题的人,你现在可以使用来自 Origin 的 Mongoid::Criteria#distinct方法从数据库中只选择不同的值:

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

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