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

时间:2022-05-07 14:31:15

Using Mongoid. Unfortunately, Mongoid does not allow for selecting unique / distinct! Have gotten these results. As you can see, there are 7 results. If you look carefully (at user_id), there are only 2 users.

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

[
  #<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')>
] 

I was looking at this, and was thinking I could do something similar so that my array would now look like 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')>
]

I'm not concerned which combination of results are extracted. As long as I have unique user_id's in the result set. Anyone know how this can be achieved?

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

6 个解决方案

#1


175  

You can just use the method uniq. Assuming your array is ary, call:

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

ary.uniq{|x| x.user_id}

and this will return a set with unique user_ids.

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

#2


15  

This should work for you:

这应该对你有用:

Consider Table1 has a column by the name of activity which may have the same value in more than one record. This is how you will extract ONLY the unique entries of activity field within Table1.

考虑Table1有一个列,它的活动名称可能在多个记录中具有相同的值。这就是在表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 

#3


11  

For those hitting this up in the future, you can now use the Mongoid::Criteria#distinct method from Origin to select only distinct values from the database:

对于将来遇到这种情况的人,您现在可以使用Mongoid::Criteria#distinct method from Origin从数据库中只选择不同的值:

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

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

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

#4


8  

Have you looked at this page?

你看过这一页了吗?

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

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

That might save you some time?

这可能会节省你一些时间?

eg db.addresses.distinct("zip-code");

如db.addresses.distinct(“你”);

#5


1  

Instead of using an Array, consider using either a Hash or a Set.

与其使用数组,不如考虑使用散列或集合。

Sets behave similar to an Array, only they contain unique values only, and, under the covers, are built on Hashes. Sets don't retain the order that items are put into them unlike Arrays. Hashes don't retain the order either but can be accessed via a key so you don't have to traverse the hash to find a particular item.

集合的行为类似于数组,只是它们只包含唯一的值,并且在幕后,建立在散列上。集合不像数组那样保留条目被放入其中的顺序。散列也不保留顺序,但是可以通过键访问,这样就不必遍历散列才能找到特定的项。

I favor using Hashes. In your application the user_id could be the key and the value would be the entire object. That will automatically remove any duplicates from the hash.

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

Or, only extract unique values from the database, like John Ballinger suggested.

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

#6


0  

Errr, it's a bit messy in the view. But I think I've gotten it to work with group (http://mongoid.org/docs/querying/)

呃,这个视图有点乱。但我想我已经让它与group合作了(http://mongoid.org/docs/querying/)

Controller

控制器

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

View

视图

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

#1


175  

You can just use the method uniq. Assuming your array is ary, call:

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

ary.uniq{|x| x.user_id}

and this will return a set with unique user_ids.

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

#2


15  

This should work for you:

这应该对你有用:

Consider Table1 has a column by the name of activity which may have the same value in more than one record. This is how you will extract ONLY the unique entries of activity field within Table1.

考虑Table1有一个列,它的活动名称可能在多个记录中具有相同的值。这就是在表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 

#3


11  

For those hitting this up in the future, you can now use the Mongoid::Criteria#distinct method from Origin to select only distinct values from the database:

对于将来遇到这种情况的人,您现在可以使用Mongoid::Criteria#distinct method from Origin从数据库中只选择不同的值:

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

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

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

#4


8  

Have you looked at this page?

你看过这一页了吗?

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

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

That might save you some time?

这可能会节省你一些时间?

eg db.addresses.distinct("zip-code");

如db.addresses.distinct(“你”);

#5


1  

Instead of using an Array, consider using either a Hash or a Set.

与其使用数组,不如考虑使用散列或集合。

Sets behave similar to an Array, only they contain unique values only, and, under the covers, are built on Hashes. Sets don't retain the order that items are put into them unlike Arrays. Hashes don't retain the order either but can be accessed via a key so you don't have to traverse the hash to find a particular item.

集合的行为类似于数组,只是它们只包含唯一的值,并且在幕后,建立在散列上。集合不像数组那样保留条目被放入其中的顺序。散列也不保留顺序,但是可以通过键访问,这样就不必遍历散列才能找到特定的项。

I favor using Hashes. In your application the user_id could be the key and the value would be the entire object. That will automatically remove any duplicates from the hash.

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

Or, only extract unique values from the database, like John Ballinger suggested.

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

#6


0  

Errr, it's a bit messy in the view. But I think I've gotten it to work with group (http://mongoid.org/docs/querying/)

呃,这个视图有点乱。但我想我已经让它与group合作了(http://mongoid.org/docs/querying/)

Controller

控制器

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

View

视图

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