Chr*_*ini 70 ruby ruby-on-rails
使用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')>
]
Run Code Online (Sandbox Code Playgroud)
我正在看这个,并且认为我可以做类似的事情,这样我的数组现在看起来像这样:
[
#<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')>
]
Run Code Online (Sandbox Code Playgroud)
我不关心提取哪种结果组合.只要我在结果集中有唯一的user_id.谁知道如何实现这一目标?
Pet*_*ter 186
你可以使用这个方法uniq
.假设您的阵列是ary
,请致电:
ary.uniq{|x| x.user_id}
Run Code Online (Sandbox Code Playgroud)
这将返回一个具有唯一user_id
s 的集合.
ajm*_*tin 15
这应该适合你:
考虑Table1有一个活动名称的列,它可能在多个记录中具有相同的值.这是您在Table1中仅提取活动字段的唯一条目的方法.
#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
Run Code Online (Sandbox Code Playgroud)
xiy*_*xiy 11
对于那些在将来遇到这种情况的人,您现在可以使用Mongoid::Criteria#distinct
Origin中的方法从数据库中仅选择不同的值:
# Requires a Mongoid::Criteria
Attendees.all.distinct(:user_id)
Run Code Online (Sandbox Code Playgroud)
http://mongoid.org/en/mongoid/docs/querying.html(v3.1.0)
你看过这个页面了吗?
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
这可能会节省你一些时间?
例如db.addresses.distinct("zip-code");