2017-12-15 34 views
0

mongodbを検索して、著者のファーストネームが "Vinod"であるすべてのパブリケーションを検索する方法はありますか?Spring mongodb @DBRefクエリーで親フィールドを検索する

は、ここに私の出版クラス

@Document(collection = "publications") 
public class Publication { 

    @Id 
    private String id; 

    private String title; 

    private Date publicationDate; 

    @DBRef 
    private Author author; 

    //getter and setters here 
} 

ですそして、私の著者クラスこれは、それがデータベースに保存されている方法です

@Document(collection = "authors") 
public class Author { 

    @Id 
    private String id; 

    @Indexed(unique = true) 
    private String username; 

    private String firstName; 

    private String lastName; 

    //getter and setters here 
} 

です。

公報

{ 
    "_id" : ObjectId("5a339cc4e193d31c47916c2c"), 
    "_class" : "com.publication.models.Publication", 
    "title" : "Some title", 
    "publicationDate" : ISODate("2017-12-15T09:58:28.617Z"), 
    "author" : { 
    "$ref" : "authors", 
    "$id" : ObjectId("5a339cc0e193d31c47916ad0") 
    } 
} 

そして著者:

{ 
    "_id" : ObjectId("5a339cc0e193d31c47916ad0"), 
    "_class" : "com.publication.models.Author", 
    "username" : "abcd0050", 
    "firstName" : "Vinod", 
    "lastName" : "Kumar" 
} 

これは私が照会する必要がある方法です。

BasicQuery query = new BasicQuery("{ author.name : 'vinod' }"); 
Publication test = mongoOperation.find(query, Publication.class); 
+0

を行うことができますどのように照会している

Query query = Query.query(new Criteria("author.name", "vinod")); Publication test = mongoOperation.find(query, Publication.class); 

を動作するはず?スプリングデータリポジトリを使用して? – pvpkiran

+0

@pvpkiran - クエリで質問を更新しました。 – VK321

答えて

0

これはあなたがまた

BasicQuery basicQuery = new BasicQuery(),addCriteria(new Criteria("author.name", "vinod")) 
+0

私はこれをテストしていませんが、私は春のクエリビルダーを探していません。フロントエンドでJquery Query Builderを使用しているので、BasicQueryクラスを使用してクエリを作成する必要があります。 http://querybuilder.js.org/demo.html – VK321

+0

BasicQueryはQueryのサブクラスです。 – pvpkiran

関連する問題