如何将一个集合中的mongo id作为外键添加到另一个集合中

时间:2017-03-24 15:49:41

标签: java mongodb spring-boot mongo-collection

在我的Spring启动应用程序中,我收集了Todos和一系列课程。在应用程序的视图中,我返回课程集合并显示我需要的任何课程。 Todos存储为1个列表,代表所有当前的Todos。我想做的是返回每门课程的待办事项列表。因此,当打开视图时,应用程序将显示该课程以及该课程的单个待办事项列表。

有没有办法可以使用现有代码来合并新功能。我已经创建了前端逻辑,并希望保留它。我最初的想法是将课程ID添加到Todo.java,但这不起作用。

Todo.java

@Document(collection="todos")
public class Todo {
@Id
private String id;

@NotBlank
@Size(max=250)
@Indexed(unique=true)
private String title;

private Boolean completed = false;  

private Date createdAt = new Date();

public Todo() {
    super();
}

public Todo(String title) {
    this.title = title;
}   

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Boolean getCompleted() {
    return completed;
}

public void setCompleted(Boolean completed) {
    this.completed = completed;
}

public Date getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

@Override
public String toString() {
    return String.format(
            "Todo[id=%s, title='%s', completed='%s']",
            id, title, completed);
}       
}

TodoRepository.java

@Repository
public interface TodoRepository extends MongoRepository<Todo, String> {
    public List<Todo> findAll();
    public Todo findOne(String id);
    public Todo save(Todo todo);
    public void delete(Todo todo);
}

课程

@Document(collection = "courses")
public class Courses {
    @Id
    private String id;
    private String name;
    private String lecturer;
    private String picture;
    private String video;
    private String description;
    private String enroled;

    public Courses(){}

    public Courses(String name, String lecturer, String picture, String video, String description,String enroled) {
        this.name = name;
        this.lecturer = lecturer;
        this.picture = picture;
        this.video = video;
        this.description = description;
        this.enroled = enroled;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLecturer() {
        return lecturer;
    }
    public void setLecturer(String lecturer) {
        this.lecturer = lecturer;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
    public String getVideo() {
        return video;
    }
    public void setVideo(String video) {
        this.video = video;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getEnroled() {
        return enroled;
    }
    public void setEnroled(String enroled) {
        this.enroled = enroled;
    }

    @Override
    public String toString() {
        return "Courses{" +
                "id='" + id + "'" +
                ", name='" + name + "'" +
                ", lecturer='" + lecturer + "'" +
                ", description='" + description + "'" +
                '}';
    }
}

0 个答案:

没有答案