首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取此数组中的唯一元素?

如何获取此数组中的唯一元素?
EN

Stack Overflow用户
提问于 2010-11-23 10:21:25
回答 5查看 109.9K关注 0票数 72

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

代码语言:javascript
复制
[
  #<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,想着我可以做一些类似的事情,这样我的数组现在看起来就像这样:

代码语言:javascript
复制
[
  #<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。有人知道如何实现这一点吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-11-23 10:37:46

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

代码语言:javascript
复制
ary.uniq{|x| x.user_id}

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

票数 193
EN

Stack Overflow用户

发布于 2013-04-03 23:26:33

对于那些将来会遇到这种情况的人,现在可以使用来自原始的Mongoid::Criteria#distinct方法从数据库中只选择不同的值:

代码语言:javascript
复制
# Requires a Mongoid::Criteria
Attendees.all.distinct(:user_id)

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

票数 11
EN

Stack Overflow用户

发布于 2010-11-23 10:38:21

你看过这个页面了吗?

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

能帮你省点时间吗?

例如db.addresses.distinct(“邮政编码”);

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4252164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档