我一直在阅读很多内容,不鼓励在Spring Data / MongoDB中使用DBRef进行集合映射。那么,我如何实现一个映射来存储从 students 集合中的那些对象中获取的ObjectId数组?
假设我有以下POJO模型:
@Document (collection = "courses")
public class Course {
@Id
private String id;
private String name;
private List<Student> students = new LinkedList<Student>();
//.. constructors, getters and setters ..
}
public interface CourseRepository extends MongoRepository<Course, String> { }
结果应该是这样的:
courses
{
_id : ObjectId("foo"),
_class: "model.Course",
name: "MongoDB for Dummies",
students: [ ObjectId("foo2"), ObjectId("foo3"), ... ]
}
而不是:
courses
{
_id : ObjectId("foo"),
_class: "model.Course",
name: "MongoDB for Dummies",
students: [
DBRef("student", ObjectId("foo2")),
DBRef("student", ObjectId("foo3"))
]
}
谢谢!
答案 0 :(得分:1)
您可能想尝试显而易见的事情并将students
更改为List<ObjectId>
。 ;)