gpt4 book ai didi

java - Spring mongodb @DBRef 查询在基本查询中搜索父字段

转载 作者:行者123 更新时间:2023-12-02 11:51:53 27 4
gpt4 key购买 nike

如何查询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);

最佳答案

我建议您使用 RelMongo,这是一个基于 Spring Data 构建的小框架,允许以 JPA 方式对 MongoDB 集合使用 @OneToMany 和 @OneToOne 注释

关于java - Spring mongodb @DBRef 查询在基本查询中搜索父字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com