我認爲這一切都取決於MongoDB集合中的JSON數據。
在你的情況下,「models」集合應該具有「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);
}
的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