【发布时间】:2017-12-15 10:03:09
【问题描述】:
如何查询 mongodb 以搜索所有作者名字为“Vinod”的出版物?
这是我的出版课
@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 - 使用查询更新问题。
标签: java spring mongodb spring-boot mongoose