你好,我有我的第一个系列
students
{
"name" : "abc"
"class" : "1"
"subjects" : DBRef("subjects","class1")
}
和我的第二个 Collection
subjects
{
"_id" : "class1"
"sub1" : "english"
"sub2" : "physics"
}
我想通过加入上述两个集合来实现我的输出
{
"name" : "abc"
"class" : "1"
"subjects" : {sub1 : "english",sub2 : "physics"}
}
有可能吗,如果有的话怎么办?
最佳答案
您可以使用@DBRef
从Student
引用Subjects
文档。
The mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.
Here's an example of using a DBRef to refer to a specific document that exists independently of the object in which it is referenced (both classes are shown in-line for brevity's sake):
编辑:-
实际上,为了向您提供更多详细信息,@DBRef
批注将急切地加载数据(即本例中的主题)。
学生模型类:-
@Document(collection = "students")
public class Students implements Serializable, BaseDocument {
private static final long serialVersionUID = -3534650012619938612L;
@Id
private String id;
@Field("class")
private String className;
@DBRef
@Field("subjects")
private Subject subject;
public String getId() {
return id;
}
public String getClassName() {
return className;
}
public void setId(String id) {
this.id = id;
}
public void setClassName(String className) {
this.className = className;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Students [id=" + id + ", className=" + className + ", subject=" + subject + "]";
}
}
主题模型类:-
@Document(collection = "subjects")
public class Subject implements Serializable, BaseDocument {
private static final long serialVersionUID = -3534650012619938612L;
@Id
private String id;
private String sub1;
private String sub2;
public String getId() {
return id;
}
public String getSub1() {
return sub1;
}
public String getSub2() {
return sub2;
}
public void setId(String id) {
this.id = id;
}
public void setSub1(String sub1) {
this.sub1 = sub1;
}
public void setSub2(String sub2) {
this.sub2 = sub2;
}
@Override
public String toString() {
return "Subject [id=" + id + ", sub1=" + sub1 + ", sub2=" + sub2 + "]";
}
}
通过 Id 获取学生:-
当你通过Id获取students时,framework/mongodb会自动加载subject数据。您无需专门执行联接即可获取主题数据。
public Students getStudents(String id) {
MongoOperations mongoOperations = getMongoConnection();
Students students = mongoOperations.findById(id, Students.class);
System.out.println(students.toString());
return students;
}
输出:-
Students [id=584ea66e9e53b7802651de36, className=1, subject=Subject [id=class1, sub1=english, sub2=physics]]
主题集合:-
{
"_id" : "class1",
"sub1" : "english",
"sub2" : "physics"
}
学生合集:-
{
"_id" : ObjectId("584ea66e9e53b7802651de36"),
"name" : "abc",
"class" : "1",
"subjects" : {
"$ref" : "subjects",
"$id" : "class1",
"$db" : "localhost"
}
}
关于java - 如何在 mongo 中加入 DBRef 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41098111/