2016-08-30 5 views
0
@Document(collection="users") 
public class User{ 
    @Id 
    private int id; 

    private String name; 
    ... 
    //getters-setters 
} 

@Document(collection="models") 
public class Model{ 
    @Id 
    private int id; 
    private String name; 
    @DBRef 
    private List<User> users; 
    ... 
    //getters-setters 
} 

나는이 솔루션을 시도했지만 아무것도 반환 나던 :봄 MongoDB를 + QueryDSL 쿼리 목록

QMODEL 모델 = 새로운 QMODEL을(); Pageable Pageable = 새 PageRequest (0, 100);

return modelsRepository.findAll (model.users.any(). id.eq (anUserId), Pageable);

+0

대답이 유용했는지 궁금합니다. – notionquest

답변

0

나는 모든 것이 MongoDB 컬렉션의 JSON 데이터에 달려 있다고 생각한다.

"모델"컬렉션의 속성은 "사용자"배열이어야합니다. 키 이름이 일치하면 (즉, '사용자') 키가 작동해야합니다.

자세한 예 : -

다음 예는 잘 작동합니다.

사람 컬렉션 -

{ 
    "_id" : ObjectId("57c8269b3ee7df409d4d2b64"), 
    "name" : "Erin", 
    "places" : [ 
     { 
      "$ref" : "places", 
      "$id" : ObjectId("57c813b33ee7df409d4d2b58")     
     } 
    ], 
    "url" : "bc.example.net/Erin" 
} 

장소 컬렉션 : -

{ 
    "_id" : ObjectId("57c813b33ee7df409d4d2b58"), 
    "name" : "Broadway Center", 
    "url" : "bc.example.net" 
} 

클래스 : -

장소 클래스 : -

@Document(collection = "places") 
public class Places implements Serializable { 

    private static final long serialVersionUID = -5500334641079164017L; 

    @Id 
    private String id; 

    private String name; 

    private String url; 

    ...get and setters 
} 

사람들 클래스 : -

@Document(collection = "people") 
public class People implements Serializable { 

    private static final long serialVersionUID = 6308725499894643034L; 

    @Id 
    private String id; 

    private String name; 

    @DBRef 
    private List<Places> places; 

    private String url; 

    ...get and setters 
} 

저장소 클래스 : -

@Repository 
public interface PeopleRepository extends PagingAndSortingRepository<People, String> { 

    public People findById(String id); 

    @Query(value = "{ 'status' : ?0 }") 
    public Page<People> findByStatus(String status, Pageable pageable); 

} 

findall은 : -

public Boolean findAllPeople() { 

     Page<People> peoplePage = peopleRepository.findAll(new PageRequest(0, 20)); 

     System.out.println("Total elements :" + peoplePage.getTotalElements()); 

     for (People people : peoplePage) { 
      System.out.println("People id :" + people.getId()); 
      System.out.println("Place size :" + people.getPlaces().size()); 
      people.getPlaces().forEach(p -> System.out.println(p.getName())); 
     } 

     return true; 

    }