나는 모든 것이 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;
}
대답이 유용했는지 궁금합니다. – notionquest