2016-08-30 18 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 
} 

私は、このソリューションを試してみましたが、それは何も返しdoesntの:春のMongoDB + QueryDSLクエリ

QModelモデル=新しいQModelを(); Pageable pageable =新しいPageRequest(0、100);

return modelsRepository.findAll(model.users.any()。id.eq(anUserId)、pageable);

+0

答えが有用かどうか疑問に思っていますか? – notionquest

答えて

0

私はそれがMongoDBコレクションのJSONデータに依存すると思います。

"models"コレクションの属性は "users" Arrayにする必要があります。キー名が一致している限り(つまり「ユーザー」)、それが機能するはずです。

具体例: -

次の例では、正常に動作します。

ピープルコレクション: -

{ 
    "_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; 

    } 
関連する問題