Programing

이 배열에서 고유 한 요소를 얻으려면 어떻게해야합니까?

lottogame 2020. 11. 3. 07:36
반응형

이 배열에서 고유 한 요소를 얻으려면 어떻게해야합니까?


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가있는 한. 누구든지 이것이 어떻게 이루어질 수 있는지 알고 있습니까?


방법을 사용할 수 있습니다 uniq. 배열이라고 가정하면 다음을 ary호출하십시오.

ary.uniq{|x| x.user_id}

그리고 이것은 고유 한 세트를 반환합니다 user_id.


이것은 당신을 위해 작동합니다.

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 

앞으로이 문제 Mongoid::Criteria#distinct를 해결하려면 Origin 메서드를 사용 하여 데이터베이스에서 고유 한 값만 선택할 수 있습니다.

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

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");


배열을 사용하는 대신 Hash 또는 Set 사용을 고려 하십시오 .

집합은 배열과 유사하게 작동하며 고유 한 값만 포함하고 내부적으로 해시를 기반으로합니다. 세트는 배열과 달리 항목이 배치되는 순서를 유지하지 않습니다. 해시는 순서를 유지하지 않지만 키를 통해 액세스 할 수 있으므로 특정 항목을 찾기 위해 해시를 탐색 할 필요가 없습니다.

나는 해시 사용을 선호합니다. 응용 프로그램에서 user_id는 키가 될 수 있고 값은 전체 개체가 될 수 있습니다. 그러면 해시에서 모든 중복 항목이 자동으로 제거됩니다.

또는 John Ballinger가 제안한 것처럼 데이터베이스에서 고유 한 값만 추출하십시오.


음,보기에 약간 지저분합니다. 하지만 그룹 (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 %>

참고 URL : https://stackoverflow.com/questions/4252164/how-do-i-get-unique-elements-in-this-array

반응형