2017-12-13 14 views
0

Mongo Java Driver를 사용 중이고 collection.find() 함수에서 필터를 사용하려고합니다. 예를 들어, 자바 객체 인 클래스가있는 클래스는 특정 필드를 포함합니다.Java 컬렉션의 Java 객체에서 변수에 액세스하는 방법은 무엇입니까?

Document document = (Document) collection.find(and(
    eq("m_seniority", key.getM_seniority()), 
    eq("m_currency",key.getM_currency()), 
    eq("m_redCode",key.getM_redCode()), 
    eq("m_companyId",key.getM_companyId()) 
)).first(); 

위의 명령을 사용합니다. 나는 대량으로, 나는 키 모음 (Collection keys를) 전달되는 오전 수행 할 때, 나는 다음과 같이 내부의 자바 객체의 특정 변수에 액세스 할 수 없습니다 게터가의없는

List<Document> docs = (List<Document>) collection.find(and(
    eq("m_seniority", keys.getM_seniority()), 
    eq("m_currency",keys.getM_currency()), 
    eq("m_redCode",keys.getM_redCode()), 
    eq("m_companyId",keys.getM_companyId()) 
)).into(new ArrayList<Document>()); 

때문에 컬렉션,하지만 개체, 나는 getters 컬렉션에 사용할 수 없습니다. 어떻게해야합니까?

List<Bson> keyFilters = new ArrayList<>(); 
// for each key create an 'and' filter on seniority, currency, redcode and companyid 
for (Key key : keys) {   
    keyFilters.add(
     Filters.and(eq("m_seniority", key.getM_seniority()), 
      Filters.eq("m_currency",key.getM_currency()), 
      Filters.eq("m_redCode",key.getM_redCode()), 
      Filters.eq("m_companyId",key.getM_companyId()) 
     ) 
    ); 
} 

List<Document> docs = (List<Document>) collection.find(
     // 'or' all of the individual key filters 
     Filters.or(keyFilters) 
).into(new ArrayList<Document>()); 
+0

당신이 샘플 문서를 제공 할 수있다 : – glytching

답변

1

Collection keys 모두에 or 쿼리를 만들려면? 그리고 아마도 위 블록 주위의 코드를 살핀다 고 할 수 있을까요? 또한 위의 두 번째 코드 샘플에서 각각의`eq()`메소드에서 "key"가 "keys"가되어야합니까? 즉 주어진 키 집합에서 임의의 키에 대한 찾기 호출을 시도하는 두 번째 블록입니까?
+0

감사합니다. 멋진 답변입니다. 한 가지 질문 만합니다. 키를 반복 할 때 https://pasteboard.co/GY7tY9O.png와 같이 Object와 Key 사이의 Type 불일치가 발생합니다. 그걸 도울 수 있니? –

+0

그리고 https://pasteboard.co/GY7At4Z.png에 표시된대로 iterating을위한 반복 가능한 유형이 필요하기 때문에 유형 변환 할 수 없습니다. –

+0

좋아요. 이제 Collection Collection에 유형을 지정하여'Collection '을 추가했는데 제대로 작동합니다. ! 고마워요! –