それはすべて、MongoDB コレクションの JSON データに依存していると思います。
あなたの場合、「モデル」コレクションには「ユーザー」配列の属性が必要です。キー名が一致している限り (つまり「users」)、動作するはずです。
詳細な例:-
次の例は問題なく動作します。
人物コレクション:-
{
"_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);
}
すべてを検索:-
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;
}