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);
を行うことができますどのように照会している
を動作するはず?スプリングデータリポジトリを使用して? – pvpkiran
@pvpkiran - クエリで質問を更新しました。 – VK321